sniperscope Posted March 9, 2009 Share Posted March 9, 2009 Hi gurus, I am trying to upload multiple images, after uploading image create a thumbnail and then copy that thumbnail to another folder. So far, my code works ok but for single file only. If i try to upload multiple images then it upload only single file and break the loops. Could anyone tell me what is wrong with my code? Regards <?php $i = 16; $j = 0; while($j<$i){ $a = $_POST['pic_id'][$j]; // Change file name. $allowed_filetypes = array('.jpg','.jpeg','.gif','.png'); //file types $upload_path = '../../img/icons/'.$id.'/grav/slides'; $upload_path = $upload_path.'/'; $filename = $_FILES['userfile']['name'][$j]; if(($filename == "iciran.jpg") != ""){ //if loop reach the last file, then upload different folder. $iciran = '../../img/icons/'.$id.'/'; move_uploaded_file($_FILES['userfile']['tmp_name'][$j],$iciran . $a); } if($filename != ""){ if(!is_writable($upload_path)){ die('You cannot upload to the specified directory, please CHMOD it to 777.'); } if(move_uploaded_file($_FILES['userfile']['tmp_name'][$j],$upload_path . $a)){ echo "Picture was" . $j . "uploaded <br>"; } else { echo 'There was an error during the file upload. Please try again.'; } ////////////////////////////////////////////// THUMBNAIL UPLOAD START /////////////////////////////////////// /// AFTER UPLOADING PICTURE GENERATE A THUMBNAIL AND COPY TO ANOTHER FOLDER /// /////////////////////////////////////////////////////////////////////////////////////////////////////////////// $src = '../../img/icons/'.$id.'/grav/slides/' .$a; $b = str_replace("p_", "t_", $a); $dst = '../../img/icons/'.$id.'/grav/thumbs/' .$b; $dstx = 85; $dsty = 107; $allowedExtensions = 'jpg jpeg gif png'; $name = explode(".", $src); $currentExtensions = $name[count($name)-1]; $extensions = explode(" ", $allowedExtensions); for($i=0; count($extensions)>$i; $i=$i+1){ if($extensions[$i]==$currentExtensions){ $extensionOK=1; $fileExtension=$extensions[$i]; break; } } if($extensionOK){ $size = getImageSize($src); $width = $size[0]; $height = $size[1]; if($width >= $dstx AND $height >= $dsty){ $proportion_X = $width / $dstx; $proportion_Y = $height / $dsty; if($proportion_X > $proportion_Y ){ $proportion = $proportion_Y; }else{ $proportion = $proportion_X ; } $target['width'] = $dstx * $proportion; $target['height'] = $dsty * $proportion; $original['diagonal_center'] = round(sqrt(($width*$width)+($height*$height))/2); $target['diagonal_center'] = round(sqrt(($target['width']*$target['width'])+ ($target['height']*$target['height']))/2); $crop = round($original['diagonal_center'] - $target['diagonal_center']); if($proportion_X < $proportion_Y ){ $target['x'] = 0; $target['y'] = round((($height/2)*$crop)/$target['diagonal_center']); }else{ $target['x'] = round((($width/2)*$crop)/$target['diagonal_center']); $target['y'] = 0; } if($fileExtension == "jpg" OR $fileExtension=='jpeg'){ $from = ImageCreateFromJpeg($src); }elseif ($fileExtension == "gif"){ $from = ImageCreateFromGIF($src); }elseif ($fileExtension == 'png'){ $from = imageCreateFromPNG($src); } $new = ImageCreateTrueColor ($dstx,$dsty); imagecopyresampled ($new, $from, 0, 0, $target['x'], $target['y'], $dstx, $dsty, $target['width'], $target['height']); if($fileExtension == "jpg" OR $fileExtension == 'jpeg'){ imagejpeg($new, $dst, 70); }elseif ($fileExtension == "gif"){ imagegif($new, $dst); }elseif ($fileExtension == 'png'){ imagepng($new, $dst); } } } } ++$j; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148579-solved-why-uploading-fails/ Share on other sites More sharing options...
Adam Posted March 9, 2009 Share Posted March 9, 2009 Try using a different loop.. foreach ($_FILES as $file) { You'd then access the array slightly different.. $filename = $file['userfile']['name']; I've not tested this.. Adam Quote Link to comment https://forums.phpfreaks.com/topic/148579-solved-why-uploading-fails/#findComment-780255 Share on other sites More sharing options...
sniperscope Posted March 9, 2009 Author Share Posted March 9, 2009 if i remove Thumbnail part then script works perfect and uploads multiple images without any problem. but i think thumbnail part makes this mess. I really want to remove it but i have to use it anyway for my little web appz. Quote Link to comment https://forums.phpfreaks.com/topic/148579-solved-why-uploading-fails/#findComment-780267 Share on other sites More sharing options...
WolfRage Posted March 9, 2009 Share Posted March 9, 2009 Perhaps you could apply the tumbnailing after the images have been uploaded? Quote Link to comment https://forums.phpfreaks.com/topic/148579-solved-why-uploading-fails/#findComment-780269 Share on other sites More sharing options...
Adam Posted March 9, 2009 Share Posted March 9, 2009 $src = '../../img/icons/'.$id.'/grav/slides/' .$a; Where does $id come from? Adam Quote Link to comment https://forums.phpfreaks.com/topic/148579-solved-why-uploading-fails/#findComment-780279 Share on other sites More sharing options...
sniperscope Posted March 9, 2009 Author Share Posted March 9, 2009 $src = '../../img/icons/'.$id.'/grav/slides/' .$a; Where does $id come from? Adam $id come from image.php file which makes a simple query from user table and get the user id and forward it to this upload page. Quote Link to comment https://forums.phpfreaks.com/topic/148579-solved-why-uploading-fails/#findComment-780336 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.