note 29936 deleted from features.file-upload by vincent
| From: | vincent@php.net | Date: | Thu, 11 Sep 2003 12:23:19 +0000 |
| Subject: | note 29936 deleted from features.file-upload by vincent | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-56111@lists.php.net to get a copy of this message | ||
Note Submitter: donkey@example.com
----
I was going barmy trying to get the upload example working on my localhost!
It was giving errors to the tune of "cannot create file, it is a directory"
Having read all the threads, I couldn't still figure out how to. Then I thought why not read
move_uploaded_file() documentation and there it was:
"bool move_uploaded_file ( string filename, string destination)
This function checks to ensure that the file designated by filename is a valid upload file (meaning
that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be
moved to the filename given by destination. "
The important thing is: "If the file is valid, it will be moved to the filename given by
destination." which makes the bulb light.
Here is my code:
<?php
// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of $_FILES.
// In PHP earlier then 4.0.3, use copy() and is_uploaded_file() instead of move_uploaded_file
$uploadfile = $_FILES['userfile']['tmp_name'];
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . '/path/to your dir/' .
$_FILES['userfile']['name'];
print "<pre>";
if (move_uploaded_file($uploadfile, $uploaddir)) {
print "File is valid, and was successfully uploaded. Here's some more debugging
info:\n";
print_r($_FILES);
} else {
print "Possible file upload attack! Here's some debugging info:\n";
print_r($_FILES);
}
?>
If you are using a windows server, refer to the related post above for '/path/to your
directory/'
As many people mentioned above, remember to do a chmod -c -v 0777 on the directory you want your
uploaded files to be moved to.
In the hope that this helps someone pulling his/her hair,
Donkey.
PS Thanks for everyone who helped by posting here.