upload file

hi, im new in php.
i'm working on this script to upload a file. im using Apache server.

here's the codes:

upload.html
...
<body>
<form enctype="multipart/form-data" action="do_upload.php" method="post">
<input type="hidden" name="max_file_size" value="1000">
Image file (1 MB max):<input name="userfile" type="file"><br>
<input type="submit" value="Upload">
</form>
</body>

do_upload.php
(the register_globals in the php.ini is turned on)

<?php

if (is_uploaded_file($userfile)) {
    copy($userfile, "/var/www/html/photolib");
} else {
    echo "Possible file upload attack. Filename: " . $userfile_name;
}

?>

When it is run, i get -
Warning: Unable to create '/var/www/html/photolib/': Is a directory in /var/www/html/photolib/do_upload.php on line 4

i also don't see the file being uploaded. what else do i need to add or re-configure?

thanks a lot.
pepperoniAsked:
Who is Participating?
 
Hamlet081299Connect With a Mentor Commented:
You should realise that this code does still leave a copy of the uploaded file in the temp directory, and I'm not sure that's really what you are after?  It also will give the file some awful name like "phpA12.tmp", which is not much use if it's a jpeg or similar!!!

What you should probably be doing is moving the temporary file that has been loaded, and saving it with the original name.

This is much better...
    move_uploaded_file($userfile, "/var/www/html/photolib/$userfile_name");


H.
0
 
Hamlet081299Commented:
looks like you should have ...

   copy($userfile, "/var/www/html/photolib/$userfile");

0
 
Hamlet081299Commented:
correction ...

  copy($userfile, "/var/www/html/photolib/" . basename($userfile));
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Hamlet081299Commented:
Other notes...

You may also want to respond differently if a file by that name exists already.

From php help "Chapter 5. Handling file uploads"...

When register_globals is turned on in php.ini the available variables are as follows. Note that the following variable names assume the use of the file upload name 'userfile', as used in the example script above:

* $userfile - The temporary filename in which the uploaded file was stored on the server machine.

* $userfile_name - The original name or path of the file on the sender's system.

* $userfile_size - The size of the uploaded file in bytes.

* $userfile_type - The mime type of the file if the browser provided this information. An example would be "image/gif".
0
 
pepperoniAuthor Commented:
hamlet, thanks.

i tried it but i got a problems with the permission..

the exact error:

Warning: Unable to create '/var/www/html/photolib/a.gif': Permission denied in /var/www/html/photolib/do_upload.php on line 7

Warning: Unable to move '/tmp/phpPeAz96' to '/var/www/html/photolib/a.gif' in /var/www/html/photolib/do_upload.php on line 7
0
 
pepperoniAuthor Commented:
hamlet, thanks.

i tried it but i got a problems with the permission..

the exact error:

Warning: Unable to create '/var/www/html/photolib/a.gif': Permission denied in /var/www/html/photolib/do_upload.php on line 7

Warning: Unable to move '/tmp/phpPeAz96' to '/var/www/html/photolib/a.gif' in /var/www/html/photolib/do_upload.php on line 7
0
 
pepperoniAuthor Commented:
hamlet, thanks.

i tried it but i got a problems with the permission..

the exact error:

Warning: Unable to create '/var/www/html/photolib/a.gif': Permission denied in /var/www/html/photolib/do_upload.php on line 7

Warning: Unable to move '/tmp/phpPeAz96' to '/var/www/html/photolib/a.gif' in /var/www/html/photolib/do_upload.php on line 7
0
 
pepperoniAuthor Commented:
sorry, i didn't mean to post the reply thrice.
0
 
dkjariwalaCommented:
Do it like,

<?php

if (is_uploaded_file($userfile))
{
   $new_filename =$HTTP_POST_FILES['userfile']['name']
   move_uploaded_file($userfile, "/var/www/html/photolib/$new_filename");
} else {
   echo "Possible file upload attack. Filename: " . $userfile_name;
}

?>

Make sure /var/www/html/photolib directory is writable by webserver.

JD
0
 
pepperoniAuthor Commented:
Thanks. its working. i changed some folder permissions.
0
 
pepperoniAuthor Commented:
Thanks. its working. i changed some folder permissions.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.