Hey,

i have this basic script for uploading files (images), the problem is I cannot upload more than two images! A part from this the script works fine, still needs a few tweaks on displaying messages for errors, but i will do that once i get the major part working.

Here is most of the script,

$numcount = count('userimage');

for($x = 0; $x <= $numcount; $x++)
	{

	$image_upload = "true";
	$complete = "false";

	$userimage_size[$x] =$_FILES['userimage']['size'][$x];
	$userimage_type[$x] =$_FILES['userimage']['type'][$x];

	if(trim($wanted[$x])==""){
		$userimage_name[$x] =$_FILES['userimage']['name'][$x];
	}
	else{
		if($userimage_type[$x] == "image/jpeg"){
			$type[$x] = ".jpeg";
		}
		elseif($userimage_type[$x] == "image/gif"){
			$type[$x] = ".gif";
		}
		elseif($userimage_type[$x] == "image/png"){
			$type[$x] = ".png";
		}
		$newname[$x] = strtolower($wanted[$x]);
		$userimage_name[$x] = ucwords(trim($newname[$x])) . $type[$x];
	}


	if($userimage_size[$x] >250000){
		$message = $message."Your Image is too large, it must be smaller than 250KB.<br>";
		$image_upload="false";
		exit();
	}

	if(!($userimage_type[$x]=="image/jpeg" or $userimage_type[$x]=="image/gif" or $iserimage_type[$x]=="imamge/png")){
		$message = $message."Your image must be either a jepg, gif or a png image.<br>";
		$image_upload="false";
		exit();
	}		


	if(!($image_upload=="false")){

		$upload[$x] = $gallery."/$userimage_name[$x]";

		move_uploaded_file($_FILES['userimage']['tmp_name'][$x],$upload[$x]);
		$complete = "true";

	}
	else{
		echo($message);
		exit();
	}
}

As i said it will only upload two images. Is it a problem with my looping structure or do i need to put something in my upload form (like. MAX_FILE_SIZE) expect for the number of files allowed.

Thanks very much.

    Try using this. I created this some time ago that should upload multiple files without a problem.

    <input type="file" name="file[]" />
    <input type="file" name="file[]" />
    <input type="file" name="file[]" />
    
    foreach($_FILES['file']['name'] as $key=>$value)
    {
       if($value != "")	
       {
          $file = $_FILES['file']['name'][$key];
          $tmpName = $_FILES['file']['tmp_name'][$key];
          $path = '/yourpathpath/';
    
      if(!move_uploaded_file($tmpName, $path . $file))
      {
         echo "Error uploading file: $file";
      }
       }
    }
    

      In that case change to input fields to

      <input type="file" name="userimage[]" />
      

        Yeah i already had that as more form, the problem was with the placement of the }

        Thanks a lot.

          Incidentally, the first line

          $numcount = count('userimage');

          would mean $numcount always equalled 1 (because there is one string to be counted). So in the loop you'd process image 0 and image 1 and that's it.

            Write a Reply...