PDA

View Full Version : PHP image upload script question


nikimere
21st October 2005, 15:50
I am trying to upload 4 images at once. First time trying this script or anything like it. Anyone let me know where i'm going wrong. it will only upload the 1st file. It 'dies' when trying the 2nd.

PHP CODE
copy ($_FILES['imagefile']['tmp_name'], "files/".$_FILES['imagefile']['name']) or die ("Could not copy file 1");
copy ($_FILES['imagefile1']['tmp_name1'], "files/".$_FILES['imagefile1']['name']) or die ("Could not copy file 2");
copy ($_FILES['imagefile2']['tmp_name2'], "files/".$_FILES['imagefile2']['name']) or die ("Could not copy file 3");
copy ($_FILES['imagefile3']['tmp_name3'], "files/".$_FILES['imagefile3']['name']) or die ("Could not copy file 4")


Prob something obvious but i'm a PHP n00b! :)
Can anyone help?

JamesF1
21st October 2005, 17:09
Your problem doesn't lie with anything in the code snippet!

the_angry_angel
21st October 2005, 17:27
Do you get any error messages when it "dies"?
What the PHP timeout set to?
Does the webserver have a timeout?

nikimere
21st October 2005, 17:58
Do you get any error messages when it "dies"?
What the PHP timeout set to?
Does the webserver have a timeout?

I dont get any error messages because i told it to print "Could not copy file 2" and so on..
Looking through my phpInfo() page i couldn't find anything on timeout except these:
mysql.connect_timeout 60
default_socket_timeout 60
it couldn't be a timeout problem because the file sizes are 18k each and its usually a quick server.

Anarchi-H
21st October 2005, 18:12
Firstly, try ...
print_r($_FILES);
... before that code snippet to make sure that the data for file 2 is actually there.

If it is there (and you are using a PHP > 4.2.0), try changing

die("Could not copy file 2")
to
die($_FILES['imagefile1']['error'])
and lookup the result here ...
http://www.php.net/manual/en/features.file-upload.errors.php

That might give you more clue as to what is occuring.

nikimere
21st October 2005, 18:43
result of print_r($_FILES);

Array ( [imagefile] => Array ( [name] => uploadTest-1.jpg [type] => image/jpeg [tmp_name] => /tmp/phpmIw4Dv [error] => 0 [size] => 18696 ) [imagefile1] => Array ( [name] => uploadTest-2.jpg [type] => image/jpeg [tmp_name] => /tmp/phpjwJ8La [error] => 0 [size] => 18696 ) [imagefile2] => Array ( [name] => uploadTest-3.jpg [type] => image/jpeg [tmp_name] => /tmp/phpozmKiZ [error] => 0 [size] => 18696 ) [imagefile3] => Array ( [name] => uploadTest-4.jpg [type] => image/jpeg [tmp_name] => /tmp/phpjg1rYc [error] => 0 [size] => 18696 ) )

result of die($_FILES['imagefile1']['error'])
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/html/carInsert.php on line 15

any help?

Anarchi-H
21st October 2005, 19:08
ehehe, I see it, and all of y'all saying there is nothing wrong with that code sample need a clout on the beak :p

You are suffixing the number on to tmp_name; you don't need to. Thats the reason the first works and the rest dont ;)

I.E. imagefile1 source location should look like
$_FILES['imagefile1']['tmp_name']
and imagefile2 source location should look like
$_FILES['imagefile2']['tmp_name']

This kinda looks like one of those errors in the Zend cert guide :p

sil3ntwar
21st October 2005, 19:27
I think copy is really only meant for copying files from one directory on the webserver to another. try using the move_uploaded_file function as it is meant for handling file uploads. Its what i use on my site and it works perfectly. Example:

move_uploaded_file($_FILES['file']['tmp_name'],'uploads/'.$_FILES['file']['name']);

Take a look at:
http://php.inspire.net.nz/manual/en/function.is-uploaded-file.php
http://php.inspire.net.nz/manual/en/function.move-uploaded-file.php

nikimere
21st October 2005, 19:33
excellent! thank you Anarchi-H .... :thumb:
was kinda obvious in the end. i though i had to give 4 different temporary names but i forgot one is executed before moving onto the other :nod:

the_angry_angel
21st October 2005, 20:51
:doh: Once again Anarchi makes me feel dumb :)

Anarchi-H
22nd October 2005, 01:07
:doh: Once again Anarchi makes me feel dumb :)

Hehe, don't feel bad... I've my fair share of *doh* moments too, only mine tend to be catastropic ones.... like writing a $500 backend for someone in ASP , using classes, only to find out that they are on a crappy old version of chillisoft that doesn't have classes :tilt:

Now that really made me feel stupid.

Mbrio
22nd October 2005, 04:12
Just a tip: when you're dealing with file uploads, be sure to check whether the file extension is valid. A php file that has an image in it will pass all the standard image checks and will also display the image, but will still execute code that might follow the image, leaving you wide open to attacks

JamesF1
22nd October 2005, 09:44
:doh: Once again Anarchi makes me feel dumb :)
You're not the only one he made feel dumb :p

I think copy is really only meant for copying files from one directory on the webserver to another. try using the move_uploaded_file function as it is meant for handling file uploads. Its what i use on my site and it works perfectly.
However, copy() is still perfectly valid for uploading files, however it was depricated with the introduction of move_uploaded_file() in PHP 4.0.3 :) But, personally, I use move_uploaded_file() now.