PHP Help

Soldato
Joined
8 Oct 2005
Posts
4,184
Location
Midlands, UK
Hi,

FIrst time I've had to do a multi file upload with PHP and need abiut of help - have a simple form with x4 fields top upload images. At the minute, the script works if i upload all 4 images, but if i upload say 2 and leave the remaining fields blank, I get header errors. Can anyone help?

PHP:
$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "/upload/".$HTTP_POST_FILES['ufile']['name'][1];
$path3= "/upload/".$HTTP_POST_FILES['ufile']['name'][2];
$path4= "/upload/".$HTTP_POST_FILES['ufile']['name'][3];
 
if (!$path1 == '') {
 copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
 $f1 = $HTTP_POST_FILES['ufile']['name'][0];
}
if (!$path2 == '') {
 copy($HTTP_POST_FILES['ufile']['tmp_name'][1], $path2);
 $f2 = $HTTP_POST_FILES['ufile']['name'][1];
}
if (!$path3 == '') {
 copy($HTTP_POST_FILES['ufile']['tmp_name'][2], $path3);
 $f3 = $HTTP_POST_FILES['ufile']['name'][2]; 
}
if (!$path4 == '') {
 copy($HTTP_POST_FILES['ufile']['tmp_name'][3], $path4);
 $f4 = $HTTP_POST_FILES['ufile']['name'][3]; 
}
header( "Location: index.php?Upload=Yes&file1=$f1&file2=$f2&file3=$f3&file4=$f4" );
 
I havn't done PHP for a long time now, but at first glance I would say we need to see what code is run when upload=Yes. I'm guessing some form of error checking needs to be in place to determine which upload fields were used and if they were blank then don't perform any actions to the non-existant file.

I could be completely wrong with this but its just a suggestion!!

That's kind of what I'm trying to do, but I've missed something, as it's still looking for blank fields :(
 
At first glance I would guess that the problem is that you are always setting the 4 variables at the top regardless of whether there is a path.

$path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
$path2= "/upload/".$HTTP_POST_FILES['ufile']['name'][1];
$path3= "/upload/".$HTTP_POST_FILES['ufile']['name'][2];
$path4= "/upload/".$HTTP_POST_FILES['ufile']['name'][3];

The $HTTP_POST_FILES will be blank but the rest will still be added to the string. Thus when you evaluate against '' then it will find that none are blank even if the file is not there and it will try and use an invalid path. E.g. $path1 would equal 'upload/' even though path 1 is left blank in the form.

Ah good idea, I'll give that a try. Thanks.

EDIT: actually that's probably it. As I'm setting the path oat the top of the script, each path will never blank. I looked at this for a good hour today and failed to see that! :) Will give this a try tomorrow morning.


I think I'd need to change the if statements to something like:

PHP:
if (!$HTTP_POST_FILES['ufile']['name'][0] == '') {
   $path1= "upload/".$HTTP_POST_FILES['ufile']['name'][0];
   copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);
   $f1 = $HTTP_POST_FILES['ufile']['name'][0];
}

.
.
. 
and so on.
 
Last edited:
Back
Top Bottom