Script Sederhana Upload File

Dibawah ini adalah script Upload File ke server.

Kita akan memanfaatkan fungsi dalam php yang sudah ada yaitu move_uploaded_file, yang berfungsi untuk memindahkan file dari local komputer kita ke server.

Saya rasa upload file suatu saat diperlukan, misal untuk mengupload gambar atau file – file aplikasi lainnya.


<?php
$uploadDir = ‘./upload/’;
if(isset($_POST['upload'])):

$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);

if (!$result):
echo “Error uploading file”;
exit;
else:
echo “upload sukses”.$filePath;
endif;

endif;

?>
<form action=”<?=$_SERVER['PHP_SELF']?>” method=”post” enctype=”multipart/form-data”>
<table width=”350? border=”0? cellpadding=”1? cellspacing=”1? class=”box”>
<tr>
<td width=”246?>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”5000000?>
<input name=”userfile” type=”file” id=”userfile”>
</td>
<td width=”80?><input name=”upload” type=”submit” class=”box” id=”upload” value=” Upload “></td>
</tr>
</table>
</form>

Leave a Reply


Verify Code   If you cannot see the CheckCode image,please refresh the page again!