RE: [PHP] More File Uploading Problems
| From: | Roger Lewis | Date: | Sat, 20 Jul 2002 16:57:16 +0000 |
| Subject: | RE: [PHP] More File Uploading Problems | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-108666@lists.php.net to get a copy of this message | ||
What a mailing list! I very much appreciate the timely and constructive
help. From all your responses, I was able to put together a solution for
uploading files to a server that worked. Here it is.
I created a file called getfiles.php in which I put the form that the user
uses to upload his file: (I've left the text and formatting out of this
post)
<form enctype="multipart/form-data" name="frmUploadFile"
action="storefile.php" method="post">
<input type="file" name="fileUpload" size="20">
<input type="submit" value="Upload this file" name="cmdSubmit">
</form>
Richard, "size" is the size of the text box into which the selected file
goes.
I created another file called storefile.php that moves
the uploaded file from the default temp directory to the assigned directory:
<?php
// In PHP earlier then 4.1.0, $HTTP_POST_FILES should be used instead of
$_FILES.
if (is_uploaded_file($HTTP_POST_FILES['fileUpload']['tmp_name'])) {
move_uploaded_file($HTTP_POST_FILES['fileUpload']['tmp_name'],
"/home/sites/home/users/demodocs/web/ProjDocs/" .
$HTTP_POST_FILES['fileUpload']['name']);
} else {
echo "Possible file upload attack. Filename: " .
$HTTP_POST_FILES['fileUpload']['name'];
}
?>
Note 1: The php manual,
http://www.php.net/manual/en/features.file-upload.php,
is confusing to us
newbies in that it is not clear that there should be a variable file name in
the path definition.
Note 2: There must be a dot '.' before the '$' in the file name. I had
omitted the dot, and accordingly got parsing error messages.
Both of the above files are put in the web site root,
/home/sites/home/users/demo/web
When I point my browser to getfiles.php the form appears and I can browse
for files from my hard drive. When I upload the file, it goes to the
default tmp
directory and is then moved to ../demodocs/web/ProjDocs
Note: I got permission denied errors until I fixed chmod for the /ProjDocs
directory. Neither 755, 666, 766 nor 622 worked. What works is 777.
I'm using PHP 4.0.6 on a Cobalt RaQ4 with Apache 1.3.20
Thanks again, all
Roger Lewis