PDA

View Full Version : Uploading images



aaron_mason
03-17-2008, 04:27 AM
Hello. I have the following html page:


<form action="fileupload.php" method="post" enctype="multipart/form-data">
<p>
<label>Select image to upload : </label>
<input type="file" name="upload" />
</p>
<p>
<input type="hidden" name="MAX_FILE_SIZE" value="25000" />
<input type="submit" value="Submit" />
</p>
</form>


and the following php file (fileupload.php):




<?
if(move_uploaded_file($_FILES['upload']['tmp_name'], "/uploads/" . $_FILES['upload'] ['name'])){
print "<p>The file has been successfully uploaded</p>";
}
else{
switch ($_FILES['upload'] ['error']){
case 1:
print '<p> The file is bigger than this PHP installation allows</p>';
break;
case 2:
print '<p> The file is bigger than this form allows</p>';
break;
case 3:
print '<p> Only part of the file was uploaded</p>';
break;
case 4:
print '<p> No file was uploaded</p>';
break;
}
}
?>


But I get this error:


Warning: move_uploaded_file(/uploads/test.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/domain/public_html/fileupload.php on line 2

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/var/tmp/phpVKMa8f' to '/uploads/test.jpg' in /home/domain/public_html/fileupload.php on line 2


Do I need to set appropriate permissions so that users can add files to a directory? Has anyone done this before with hostmonster? What needs correcting?

Please help!

Thanks

aaron_mason
03-17-2008, 04:38 AM
Linfox has already answered this question very very well.

http://www.hostmonsterforum.com/showthread.php?t=2758

linFox
03-17-2008, 04:57 AM
Yeah, the script I posted in that topic should work fine (in fact I've made a much improved version of the same thing; I may post it some time).

The main reason yours is failing though is you haven't added your own account directory prefix to the destination location.

When you look at your files through FTP, File Manager etc, you see your root path (the base path that contains public_html, etc) as /.
This path on the server is in fact /home/username/, username being your own account username (so your public_html has an actual path of /home/username/public_html/).
You are trying to move your upload to /uploads/test.jpg, which fails because you are only allowed to write files to your own file space (obviously).
You instead want the path to be /home/username/public_html/uploads/test.jpg

This prefix that you need to add (/home/username/public_html) is available from the DOCUMENT_ROOT environment variable, so you don't have to specifiy it manually. In PHP, you would use $_SERVER['DOCUMENT_ROOT'].

What it all comes down to is simply replacing the line:

if(move_uploaded_file($_FILES['upload']['tmp_name'], "/uploads/" . $_FILES['upload'] ['name'])){
with:


if(move_uploaded_file($_FILES['upload']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/uploads/" . $_FILES['upload'] ['name'])){
This now points to the uploads folder within your public_html.

aaron_mason
03-17-2008, 07:15 AM
Thank you.

By the way, I would be very interested in seeing the much improved version! The one you have provided already is brilliant.