How to trouble shoot file upload?

I try to load one image and I get

  1. $file unknown type
  2. unknown type

Is there something I’m not seeing? Thanks.


<?php
define('MAX_FILE_SIZE', 100000);
define('UPLOAD_DIR', 'upload_test/');

if (array_key_exists('upload', $_POST)) {

  foreach ($_FILES['image']['name'] as $number => $file) {
  
      $file = str_replace(' ', '_', $file);
	  $sizeOK = false;
          
		if ($_FILES['image']['size'][$number] > 0 || $_FILES['image']['size'][$number] <= MAX_FILE_SIZE) {
    $sizeOK = true;
	}
		  if (!$sizeOK) {
		    $result[] = "$file size is too large";
			}
         
          $typeok = false;       
          switch ($_FILES['image']['type'][$number])
          {
              case "image/gif":
                  $src = imagecreatefromgif($file);
				  $typeok = true;
                  break;                  
              case "image/jpeg":
                  // Both regular and progressive jpegs 
              case "image/pjpeg":
                  $src = imagecreatefromjpeg($file);
				  $typeok = true;
                  break;                  
              case "image/png":
                  $src = imagecreatefrompng($file);
				  $typeok = true;
                  break;                  
              default:
          }

		  if (!$typeOK) {
		    $result[] = "$file unknown type";
			}
			
		  if ($sizeOK && $typeOK) {
			$success = move_uploaded_file($file, UPLOAD_DIR . $udfile);
			}
			
          if ($success)
          {
              list($w, $h) = getimagesize($udfile);
              $max = 100;
              $tw = $w;
              $th = $h;             
              if ($w > $h && $max < $w)
              {
                  $th = $max / $w * $h;
                  $tw = $max;
              }
              elseif ($h > $w && $max < $h)
              {
                  $tw = $max / $h * $w;
                  $th = $max;
              }
              elseif ($max < $w)
              {
                  $tw = $th = $max;
              }              
              $tmp = imagecreatetruecolor($tw, $th);
              imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
              imageconvolution($tmp, array(// Sharpen image 
              array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
              imagejpeg($tmp, $udfile);
              imagedestroy($tmp);
              imagedestroy($src);
          }
  }
}
?>

<?php
if (isset($result)) {
  echo '<ol>';
  foreach ($result as $item) {
    echo "<strong><li>$item</li></strong>";
	}
  echo '</ol>';
  }
?>
<form action="" method="post" enctype="multipart/form-data" name="multiUpload" id="multiUpload">
    <p>
        <label for="image1">File 1:</label>
		<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
        <input type="file" name="image[]" id="image1" />
    </p>
    <p>
        <label for="image2">File 2:</label>
        <input type="file" name="image[]" id="image2" />
    </p>
    <p>
        <input name="upload" type="submit" id="upload" value="Upload files" />
    </p>
</form>

Thanks for your response. I made some changes to the script, move upload file tmp name just above the switch function.

I still get the same (unknown type) error message. This time the file is moved but no change (scaling) to it has been done.

Am I understanding you correctly?


<?php
define('MAX_FILE_SIZE', 100000);
define('UPLOAD_DIR', 'upload_test/');

if (array_key_exists('upload', $_POST)) {

  foreach ($_FILES['image']['name'] as $number => $file) {
  
      $filenm = str_replace(' ', '_', $file);
	  $sizeOK = false;
          
		if ($_FILES['image']['size'][$number] > 0 || $_FILES['image']['size'][$number] <= MAX_FILE_SIZE) {
    $sizeOK = true;
	}
		  if (!$sizeOK) {
		    $result[] = "$filenm size is too large";
			}
         
		  move_uploaded_file($_FILES['image']['tmp_name'][$number], UPLOAD_DIR . $filenm);
          $udfilenm = UPLOAD_DIR . $filenm;
		 
          $typeok = false;       
          switch ($_FILES['image']['type'][$number])
          {
              case "image/gif":
                  $src = imagecreatefromgif($udfilenm);
				  $typeok = true;
                  break;                  
              case "image/jpeg":
                  // Both regular and progressive jpegs 
              case "image/pjpeg":
                  $src = imagecreatefromjpeg($udfilenm);
				  $typeok = true;
                  break;                  
              case "image/png":
                  $src = imagecreatefrompng($udfilenm);
				  $typeok = true;
                  break;                  
              default:
          }

		  if (!$typeOK) {
		    $result[] = "$file unknown type";
			}
			

		  if ($sizeOK && $typeOK)
          {
              list($w, $h) = getimagesize($udfilenm);
              $max = 100;
              $tw = $w;
              $th = $h;             
              if ($w > $h && $max < $w)
              {
                  $th = $max / $w * $h;
                  $tw = $max;
              }
              elseif ($h > $w && $max < $h)
              {
                  $tw = $max / $h * $w;
                  $th = $max;
              }
              elseif ($max < $w)
              {
                  $tw = $th = $max;
              }              
              $tmp = imagecreatetruecolor($tw, $th);
              imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
              imageconvolution($tmp, array(// Sharpen image 
              array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1)), 8, 0);
              imagejpeg($tmp, $udfilenm);
              imagedestroy($tmp);
              imagedestroy($src);
          }
  }
}
?>

<?php
if (isset($result)) {
  echo '<ol>';
  foreach ($result as $item) {
    echo "<strong><li>$item</li></strong>";
	}
  echo '</ol>';
  }
?>
<form action="" method="post" enctype="multipart/form-data" name="multiUpload" id="multiUpload">
    <p>
        <label for="image1">File 1:</label>
		<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
        <input type="file" name="image[]" id="image1" />
    </p>
    <p>
        <label for="image2">File 2:</label>
        <input type="file" name="image[]" id="image2" />
    </p>
    <p>
        <input name="upload" type="submit" id="upload" value="Upload files" />
    </p>
</form>

Warning: imagecreatefromjpeg(Upppp.jpg) [function.imagecreatefromjpeg]: failed to open stream: No such file or directory in mysite.blabla.php

I think I found a clue. Inside the switch function

case “image/jpeg”:
$src = imagecreatefromjpeg($file);

$file is just a name. It may need to be $file = SOURCE_DIR . $_FILES[‘image’][‘name’]
before $file enters the switch function.

What I do not understand is a SOURCE_DIR is a path to file inside my computer or on a website server. For example:

define(‘SOURCE_DIR’, ‘C:/htdocs/myfolder/’);

But the image is being uploaded from a visitor to my site, so wouldn’t the SOURCE_DIR be every persons computer who visits and uploads onto my site?

Thanks.

File uploads are placed in a temporary location on the server. The actual path to the temporary file may be found under the key tmp_name inside the $_FILES array. The name key has no functional purpose beyond providing the original name of the file.

I just had a break through.

  1. Some of my variables were inconsistently named: $typeOK and $typeok.
    Now the script moves and scales the image.

  2. When I upload just one image successfully I get the error below. Now I realize that means the first image is 0 and the second image which there was none is 1. Thus an error is displayed when I choose only one image. So now I have to figure out how to display only one error for the one an only file being uploaded.

  3. upload_test/ unknown type

This script uploads and scales two image files.

I should point out that the $file unknown type is an error statement.

The program stops at the end of the switch statement.

if (!$typeOK) {
$result = “$file unknown type”;
}

I try gif. or jpg. and I get the same error.