zondag 15 maart 2009

Uploading with PHP

The form
1. make sure you have a form with a file field:
<form action="upload.php" enctype="multipart/form-data" method="POST" >
<input type="hidden" value="30000" name="MAX_FILE_SIZE">
<input type="file" name="userfile">
<input type="submit" value="Upload now">
</form>


The script

$target = "uploads/";
$target = $target. filename_safe(basename($_FILES["userfile"]["name"]));

if(move_uploaded_file($_FILES["userfile"]["tmp_name"], $target)) {
// upload succesfull
echo "upload successful";
}
else {
// upload not successfull
echo "upload unsuccesful";
}


Extra information

If things don't work correctly:

* check for the right PHP version: echo phpversion();
From PHP 4.10 you can use $_FILES, otherwise use $HTTP_POST_FILES.

* the actual files you try to upload may be too big, try it with smaller files!