Forum Moderators: coopster

Message Too Old, No Replies

Problem with uploading images to dir

         

dwighty

12:58 pm on Dec 27, 2006 (gmt 0)

10+ Year Member



Hi Guys,

I have a problem with the below script. I want to be able to upload 3 images to the server and then record their names into a database along with other details submitted in the form.

The names are recorded in the db correctly and I am not getting any errors for the upload (apparently!) - Checked using print_r($_FILES)
Any help would be appreciated.

/* CODE */
# Main Dir
$uploaddir = $_SERVER['DOCUMENT_ROOT'] . $directory_self . '/images/cars/';
$uploadfile1 = $uploaddir . basename($_FILES['add_car_image_1']['name']);
$uploadfile2 = $uploaddir . basename($_FILES['add_car_image_2']['name']);
$uploadfile3 = $uploaddir . basename($_FILES['add_car_image_3']['name']);

if( (move_uploaded_file($_FILES['add_car_image_1']['tmp_name'], $uploadfile1)) ¦¦ (move_uploaded_file($_FILES['add_car_image_2']['tmp_name'], $uploadfile2)) ¦¦ (move_uploaded_file($_FILES['add_car_image_3']['tmp_name'], $uploadfile3)) ){

$upimage1 = $_FILES['add_car_image_1']['name'];
$upimage2 = $_FILES['add_car_image_2']['name'];
$upimage3 = $_FILES['add_car_image_3']['name'];

$query="INSERT INTO ".$site_prefix."_site_cars ...etc...

} else {
echo 'There has been a problem with the Upload';
}

Thanks

Paul

mcibor

2:10 pm on Dec 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With this:

if( (move_uploaded_file($_FILES['add_car_image_1']['tmp_name'], $uploadfile1)) ¦¦ (move_uploaded_file($_FILES['add_car_image_2']['tmp_name'], $uploadfile2)) ¦¦ (move_uploaded_file($_FILES['add_car_image_3']['tmp_name'], $uploadfile3)) ){

you are checking if at least one file uploads correctly

With

if( (move_uploaded_file($_FILES['add_car_image_1']['tmp_name'], $uploadfile1)) && (move_uploaded_file($_FILES['add_car_image_2']['tmp_name'], $uploadfile2)) && (move_uploaded_file($_FILES['add_car_image_3']['tmp_name'], $uploadfile3)) ){

all the files have to be uploaded to be correct.

However I would do sth else even:

<input type="file" name="add_car_image[]">
and then

you have files in an array
$_FILES['add_car_image'][0]['tmp_name']
$_FILES['add_car_image'][1]['tmp_name']

what you can do is

foreach ($_FILES['add_car_image'] as $car_image)
{
$uploadfile = $uploaddir . basename($car_image['name']);
if(move_uploaded_file($car_image['tmp_name'], $uploadfile)
{
$upimage = $car_image['name'];
$query="INSERT INTO ".$site_prefix."_site_cars ...etc... //or even perform multiinsert
}
}

Hope this helps you.

I couldn't find a simple error in your script, so try to process the files indepedently

Michal

dwighty

2:20 pm on Dec 27, 2006 (gmt 0)

10+ Year Member



Thanks mcibor.

I tried both && and ¦¦ but got the same results. I also tried the add_car_image[] but could not get this to work.

I will give it another try.

With the method i posted, if i used it to upload 1 single image it works fine.

mcibor

2:28 pm on Dec 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What does say:

if(move_uploaded_file($_FILES['add_car_image_1']['tmp_name'], $uploadfile1) echo "File 1 OK<br />"
else "Error in uploading file 1<br />;

if(move_uploaded_file($_FILES['add_car_image_2']['tmp_name'], $uploadfile2) echo "File 2 OK<br />"
else "Error in uploading file 2<br />;

if(move_uploaded_file($_FILES['add_car_image_3']['tmp_name'], $uploadfile3) echo "File 3 OK<br />"
else "Error in uploading file 3<br />;
echo "<br />File 1: $uploadfile1<br />File 2: $uploadfile2<br />File 3: $uploadfile3<br />";
?
Any errors? Anything suspicious in files

dwighty

2:59 pm on Dec 27, 2006 (gmt 0)

10+ Year Member



mcibor

Thanks for your help, not sure what I was not quite getting but instead of using

if( (move_uploaded_file($_FILES['add_car_image_1']['tmp_name'], $uploadfile1)) && (move_uploaded_file($_FILES['add_car_image_2']['tmp_name'], $uploadfile2)) && (move_uploaded_file($_FILES['add_car_image_3']['tmp_name'], $uploadfile3)) ){

I just seperated them up.

Thanks again,

Paul