...

View Full Version : Resolved uploading jpg three times to three different locations



illusia.it
03-19-2010, 01:57 PM
Hello, i've searched the site and the web for this but i could not find anything.

what i need is when i upload 1.jpg file onto the server using $_FILES[] i need at the same time to save the same file to the following folders:

-photos/
-photos/thumbs/
-photos/big/

here's my script so far (simplified):


//defining variables
$imagename = "1.jpg";
$source = $_FILES['foto1']['tmp_name'];
$target = "../photos/".$imagename;

// 1st time target path is just photos
move_uploaded_file($source, $target);
imagejpeg($imagename, $target, 100);

// 2nd time target includes 'big'
$target = "../photos/big/".$imagename;
move_uploaded_file($source, $target);
imagejpeg($imagename, $target, 100);

// 3rd time target includes 'thumbs'
$target = "../photos/thumbs/".$imagename;
move_uploaded_file($source, $target);
imagejpeg($imagename, $target, 100);

it simply doesnt work, i hope that was clear enough, thank you in advance!
regards!

illusia.it
03-19-2010, 02:04 PM
still me, i mean it does save 1.jpg into photos/ but it doesnt save the same image into the other 2 folders!!! thanks again

abduraooft
03-19-2010, 02:07 PM
// 2nd time target includes 'big'
$target = "../photos/big/".$imagename;
move_uploaded_file($source, $target);
imagejpeg($imagename, $target, 100);

// 3rd time target includes 'thumbs'
$target = "../photos/thumbs/".$imagename;
move_uploaded_file($source, $target);
imagejpeg($imagename, $target, 100); You can't use move_uploaded_file() more than once to read from the same source, as the file will not be there after first move (it's not like a copy).

Thus for the second and third block, use the target of initial block as the source and use the imagejpeg() to make new files(makes sure that it won't overwrite the existing one). After that use the functions copy() and unlink() (to delete after copying to the targets)

illusia.it
03-19-2010, 05:26 PM
You can't use move_uploaded_file() more than once to read from the same source, as the file will not be there after first move (it's not like a copy).

Thus for the second and third block, use the target of initial block as the source and use the imagejpeg() to make new files(makes sure that it won't overwrite the existing one). After that use the functions copy() and unlink() (to delete after copying to the targets)


:) Thank you very much, that is exactly what i came up with a moment ago, i figured it out, only it was a little more complicated because the images of each folder had to have a specific size, here is the code i came up with, hopig it will help someone (as non experienced as me):



$counter = 1;
foreach($_FILES as $key){ // loops as many times as the files are (9)
if(!$_FILES['foto'.$counter.'']['error']){ // but saves the picture only as many as the files uploaded were

$imagename = $counter.".jpg";
$source = $_FILES['foto'.$counter.'']['tmp_name'];
$target = "../photos/carpenteria_strutture".$id."/big/".$imagename;
$target2 = "../photos/carpenteria_strutture".$id."/".$imagename;
$target3 = "../photos/carpenteria_strutture".$id."/thumbs/".$imagename;

move_uploaded_file($source, $target);

$target = "../photos/carpenteria_strutture".$id."/big/".$imagename;
list($width, $height) = getimagesize($target);
if($height>$width){
$modwidth=450;
}else{
$modwidth=800;
}
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($target);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $target, 100);

// copy files from big and paste them into ../ and thumbs/
// then pick them up and resize them
copy($target, $target2);
copy($target, $target3);

list($width, $height) = getimagesize($target2);
if($height>$width){
$modwidth=101;
}else{
$modwidth=180;
}
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($target);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $target2, 100);

list($width, $height) = getimagesize($target3);
if($height>$width){
$modwidth=32;
}else{
$modwidth=57;
}
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($target);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $target3, 100);

}
$counter++; // counter keeps counting regardless (9 times)
}

abduraooft
03-19-2010, 05:33 PM
hopig it will help someone (as non experienced as me): Nice! I'd recommend to edit your post and add
][/COLOR] tags arounf your php code.

illusia.it
03-19-2010, 06:17 PM
Nice! I'd recommend to edit your post and add
][/COLOR] tags arounf your php code.

Yes, Thank you for that, it looks so much better now! however i could not find a way to edit my previous post but here is the code again :thumbsup:




$counter = 1;
foreach($_FILES as $key){ // loops as many times as the files are (9)
if(!$_FILES['foto'.$counter.'']['error']){ // but saves the picture only as many as the files uploaded were

$imagename = $counter.".jpg";
$source = $_FILES['foto'.$counter.'']['tmp_name'];
$target = "../photos/carpenteria_strutture".$id."/big/".$imagename;
$target2 = "../photos/carpenteria_strutture".$id."/".$imagename;
$target3 = "../photos/carpenteria_strutture".$id."/thumbs/".$imagename;

move_uploaded_file($source, $target);

$target = "../photos/carpenteria_strutture".$id."/big/".$imagename;
list($width, $height) = getimagesize($target);
if($height>$width){
$modwidth=450;
}else{
$modwidth=800;
}
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($target);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $target, 100);

// copy files from big and paste them into ../ and thumbs/
// then pick them up and resize them
copy($target, $target2);
copy($target, $target3);

list($width, $height) = getimagesize($target2);
if($height>$width){
$modwidth=101;
}else{
$modwidth=180;
}
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($target);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $target2, 100);

list($width, $height) = getimagesize($target3);
if($height>$width){
$modwidth=32;
}else{
$modwidth=57;
}
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($target);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $target3, 100);

}
$counter++; // counter keeps counting regardless (9 times)
}

illusia.it
03-19-2010, 06:24 PM
oo one more thing.. i was finally able to edit the previous post, that's weird but i could have sworn the edit button was not visible, now i see it o well.

hey and i was looking for a way to mark this thread as closed/resolved so that i will be able to do it in the future too.. thank you so much for your help, god bless! :)

abduraooft
03-19-2010, 06:32 PM
See #6 of http://www.codingforums.com/postguide.htm!

PS: There's a "delete" button too, if you think one of your post is not required :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum