Membuat sendiri Photo Uploader dengan PHP
June 14, 2008 by Adam Pahlevi
Filed under Mr. WordPress
Inilah contoh script dari PHP yang menunjukkan bagaimana kita bisa meng-upload (mengirim) photo kepada pemilik situs tersebut, dan jika kita punya teknik programming PHP dan bisa memodifnya sedikit, maka kita bisa membuat script itu tidak untuk mengirim gambar, seperti mengirim aplikasi, lagu, dan lain sebagainya.
Pertama-tama, kita membuat file dengan nama kirim-terima.php, yang berisi:
//if they DID upload a file…
if($_FILES['photo']['name'])
{
//if no errors…
if(!$_FILES['photo']['error'])
{
//now is the time to modify the future file name and validate the file
$new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
if($_FILES['photo']['size'] > (1024000)) //can’t be larger than 1 MB
{
$valid_file = false;
$message = ‘Oops! Your file\’s size is to large.’;
}//if the file has passed the test
if($valid_file)
{
//move it to where we want it to be
move_uploaded_file($_FILES['photo']['tmp_name'], ‘uploads/’.$new_file_name);
$message = ‘Congratulations! Your file was accepted.’;
}
}
//if there is an error…
else
{
//set that to be the returned message
$message = ‘Ooops! Your upload triggered the following error: ‘.$_FILES['photo']['error'];
}
}//you get the following information for each file:
$_FILES['field_name']['name']
$_FILES['field_name']['size']
$_FILES['field_name']['type']
$_FILES['field_name']['tmp_name']
Setelah itu, kita hanya tinggal membuat form untuk penguploadan saja, Form-nya akan berkode seperti ini:
<form action=”kirim-terima.php” method=”post” enctype=”multipart/form-data”>
Pilih photo: <input type=”file” name=”photo” size=”25″ />
<input type=”submit” name=”submit” value=”Kirim” />
</form>

Comments
Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!