Thursday, September 23, 2010

Upload



Upload form:
<form action="uploadprocess.php" method="post" enctype="multipart/form-data">
<table width="200" border="0">
  <tr>
    <td>
<input type="file" name="upload" /></td>
  </tr>
  <tr>
    <td><label>
      
<input type="submit" name="submit" value="Save" />
    
</label></td>
  </tr>

</table>
</form>


Upload Script:
$upload=$_FILES['upload']['name'];//nama fail yang akan di dimuatnaik dan disimpan di dalam database/ the name of the file that will be uploaded and stored in database
$target='pictures/';//target dimana fail akan di simpan iaitu folder 'pictures'/ the target where the file will be stored that is in folder 'pictures'

$target=$target.basename($_FILES['upload']['name']);

if($_FILES['
upload']['size'] >= 10000000){//sekiranya saiz fail melebihi 10mb, error mesej akan muncul/ if the size of the file is exceeded 1MB, error message will appear
 echo "file size over limit";//error mesej/ error message
   }
elseif($_FILES['upload']['type'] != 'application/jpeg'){//sekiranya jenis fail selain jpeg error mesej akan muncul/ if the type of the file is not in jpeg format, error message will appear
 echo "invalid file type";//error mesej/ error message
   }
else{
  mysql_query("INSERT INTO pics (mypic) VALUES ('$upload')");//nama fail akan disimpan di dalam database/ name of file stored in 'pics' table
  move_uploaded_file($_FILES['upload']['tmp_name'], $target);//fail yang telah dimuat naik akan di simpan ke folder 'pictures'/ the file that has been uploaded will be stored in folder 'pictures'
    }
Note: as we can see on the script, only the name of file will be stored in the database. the file itself will be stored in the folder 'pictures' that been created before.

____________________________________________________________________
Sekiranya fail yang hendak diupload lebih dari 1 fail:
Upload multiple file:
perlu tambah '[]' pada 'input name' 
need to add '[]' at the end of 'input name'
<form action="uploadprocess.php" method="post" enctype="multipart/form-data">
<table width="200" border="0">
  <tr>
    <td>
<input type="file" name="upload[]" /></td>
  </tr>
  <tr>
    <td><label>
      
<input type="submit" name="submit" value="Save" />
    
</label></td>
  </tr>
</form


Upload Script:
$arr=array();
foreach($_FILES['upload']['name'] as $a => $b){ /menggunakan fungsi switch untuk data yang banyak/ using function switch for multiple data
$arr=$b;

$target='pictures/';//target dimana fail akan di simpan iaitu folder 'pictures'
$target=$target.basename($_FILES['upload']['name']);

if($_FILES['upload']['size'] >= 10000000){//sekiranya saiz fail melebihi 10mb, error mesej akan muncul
echo "file size over limit";//error mesej
   }
elseif($_FILES['upload']['type'] != 'application/jpeg'){//sekiranya jenis fail selain jpeg error mesej akan muncul
 echo "invalid file type";//error mesej
   }
else{
  mysql_query("INSERT INTO pics (mypic) VALUES ('$b')");//nama fail akan disimpan di dalam database
  move_uploaded_file($_FILES['upload']['tmp_name'], $target);//fail yang telah dimuat naik akan di simpan ke folder 'pictures'
   
}
______________________________________________________________________
Bina folder 'subfoder' di dalam foder 'pictures' secara automatik apabila file diupload:
Create folder 'subfolder' inside folder 'pictures' automatically while files being uploaded:

here is the script to create 'subfolder':
$dir='pictures/';
mkdir($dir,'subfolder', 0777) or die ('could not create directory');
chmod($dir,'subfolder', 0777);


$arr=array();
foreach($_FILES['upload']['name'] as $a => $b){
$arr=$b;

$target='pictures/subfolder/';//target dimana fail akan di simpan iaitu folder 'subfolder'
$target=$target.basename($_FILES['upload']['name']);

if($_FILES['upload']['size'] >= 10000000){//sekiranya saiz fail melebihi 10mb, error mesej akan muncul
echo "file size over limit";//error mesej
}
elseif($_FILES['upload']['type'] != 'application/jpeg'){//sekiranya jenis fail selain jpeg error mesej akan muncul
 echo "invalid file type";//error mesej
   }
else{
  mysql_query("INSERT INTO pics (mypic) VALUES ('$b')");//nama fail akan disimpan di dalam database
  move_uploaded_file($_FILES['upload']['tmp_name'], $target);//fail yang telah dimuat naik akan di simpan ke folder 'pictures'
   
}

No comments:

Post a Comment