Copy file problem

Links for php scripts

Moderators: macek, egami, gesf

KasunL
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Apr 13, 2012 2:04 am

Copy file problem

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

Hi!

i'm a beginner and i've created this small commenting page: http://slprogrammersheaven.zzl.org/test1/index.php. 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 />";
    }
  else
    {
    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 />";
...
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "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.

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

Re: Copy file problem

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

Inside of move_uploaded_file() there is

Code: Select all

$_FILES["file"]["bg004x.jpg"] 

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

Reference:
$_FILES
var_dump()


Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 1 guest