Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

Copy file problem

Links for php scripts

Moderators: macek, egami, gesf

Copy file problem

Postby KasunL » Fri Apr 13, 2012 2:45 am


i'm a beginner and i've created this small commenting page: There's a link at the bottom of the page (which is currently not working) to change the background image of the webpage. When the link is clicked, a page is opened containing the controls to upload an image file. When the Submit button is pressed, upload_file.php file is called to handle the uploading job. The code should 1st upload the file and then copy it to the "upload" directory as "bg004x.jpg". The code:
Code: Select all
if ($_FILES["file"]["error"] > 0)
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
      "upload\\" . $_FILES["file"]["bg004x.jpg"]); // Move the file

When i run this on localhost, i get below output with errors:
Code: Select all
Notice: Undefined index: bg004x.jpg in C:\xampp\htdocs\test1\upload_file.php on line 26

Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in C:\xampp\htdocs\test1\upload_file.php on line 26

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php55A0.tmp' to 'upload\' in C:\xampp\htdocs\test1\upload_file.php on line 26

What's causing these errors? :help:

Thanks for any help.
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Apr 13, 2012 2:04 am

Re: Copy file problem

Postby minimihi » Sun Apr 15, 2012 1:29 pm

Inside of move_uploaded_file() there is
Code: Select all

Variable $_FILES doesn't have such key as "bg004x.jpg", that should be "name"

All 3 errors are coming from there.

After knowing a little how to program, you need to learn how to debug your code.
It's 3 times that you where told where error is 8) on 26th line

try putting
Code: Select all
echo "<pre>"; var_dump($_FILES); echo "</pre>"; 

before if or inside of else (before move_uploaded_file()) statement and see what you will get

User avatar
New php-forum User
New php-forum User
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.