Suatu ketika form index tidak dapat melembar nilai dari file field, ternyata kurang script enctype="multipart/form-data"
pada form.
"upload.php".
<form enctype="multipart/form-data" action="upload.php"
method="post">
<input type="hidden" name="MAX_FILE_SIZE"
value="50000" />
Select a File:<br />
<input type="file" size="20" name="thefile" /><br />
<input type="submit" name="Submit" value="Submit" />
</form>
UPLOAD HANDLER
if(array_key_exists('thefile', $_FILES)) {
// Validate the uploaded file
if($_FILES['thefile']['size'] === 0
|| empty($_FILES['thefile']['tmp_name'])) {
echo("<p>No file was selected.</p>\r\n");
} else if($_FILES['thefile']['size'] > 50000) {
echo("<p>The file was too large.</p>\r\n");
} else if($_FILES['thefile']['error'] !== UPLOAD_ERR_OK) {
// There was a PHP error
echo("<p>There was an error uploading.</p>\r\n");
} else {
// Create uploads directory if necessary
if(!file_exists('uploads')) mkdir('uploads');
// Move the file
if(move_uploaded_file($_FILES['thefile']['tmp_name'],
'uploads/' . $_FILES['thefile']['name'])) {
echo("<p>File uploaded successfully!</p>\r\n");
} else {
echo("<p>There was an error moving the file.</p>\r\n");
}
}
}
Source: http://www.dbscripts.net/tutorials/php-file-upload/ |