I'm trying to upload and thumbnail 3 pictures in a shot.
The form is as follows:
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
<input name="userfile[]" type="file" /><br />
So I'm using an array to send all pictures, after I try to acces it with a for loop:
The count($userfile) should maybe look better like count($_FILES['userfile']) but the fact is that only works in the first case.
If I echo them, only count($userfile) gives me the right result.
Too many weird things...
Check my code, please:
$totalimages=count($userfile);
if($totalimages != 0){
for($a=0;$a<$totalimages;$a++){
$msg = "";
switch($_SERVER['REQUEST_METHOD']) {
case true:
if(!isset($_FILES['userfile'][$a]) || $_FILES['userfile'][$a] == "none" || $_FILES['userfile'][$a] == "") {
$msg = "Press browse to select a picture";
break;
}
$tmp = getcwd()."/".$_FILES['userfile'][$a]['name'];
if(!@move_uploaded_file($_FILES['userfile'][$a]['tmp_name'], $tmp)) {
$msg = "There was an error while uploading";
break;
}
$fp = fopen($tmp, "rb");
$str = fread($fp, filesize($tmp));
fclose($fp);
unlink($tmp);
$im1 = ImageCreateFromString($str);
$imgname = $ref."_thumb_".$a;
$maxwidth =250;
$maxheight = 200;
$width1 = ImageSX($im1);
$height1 = ImageSY($im1);
$width2 = $maxwidth;
$height2 = floor(($width2 * $height1) / $width1);
if($maxheight > 0 && $height2 > $maxheight) {
$height2 = $maxheight;
$width2 = floor(($height2 * $width1) / $height1);
}
$im2 = ImageCreateTrueColor($width2, $height2);
ImageCopyResampled($im2, $im1, 0, 0, 0, 0, $width2, $height2, $width1, $height1);
ImageJpeg($im2, "thumb/".$imgname.".jpg"); // Se copia en temp/
$msg = "Ok";
$im3 = ImageCreateFromString($str);
$imgname2 = $ref."_full_".$a;
$maxwidth2 = 500;
$maxheight2 = 300;
$width12 = ImageSX($im3);
$height12 = ImageSY($im3);
$width22 = $maxwidth2;
$height22 = floor(($width22 * $height12) / $width12);
if($maxheight2 > 0 && $height22 > $maxheight2) {
$height22 = $maxheight2;
$width22 = floor(($height22 * $width12) / $height12);
}
$im4 = ImageCreateTrueColor($width22, $height22);
ImageCopyResampled($im4, $im3, 0, 0, 0, 0, $width22, $height22, $width12, $height12);
ImageJpeg($im4, "full/".$imgname2.".jpg");
ImageDestroy($im1);
ImageDestroy($im2);
ImageDestroy($im3);
ImageDestroy($im4);
break;
}}