Jump to content

[SOLVED] Why uploading fails ?


sniperscope

Recommended Posts

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;
	}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.