尝试查找图像大小时出现未定义的索引错误


Undefined index error when trying to find image size

我想用getimagesize找到上传的图像大小,但我得到了这个错误:

未定义的索引:第47行C:'wamp'www'cbir'process.php中的image

给出错误的代码行:

$info = getimagesize($_FILES['image']['tmp_name']);
$img = imagecreatefromjpeg($_FILES['image']['tmp_name']);

整个代码:

<form action="process.php" method="post" enctype="multipart/form-data">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<!-- Name of input element determines name in $_FILES array -->
<input name="userfile" type="file" id="userfile">
<input name="upload" type="submit" id="upload" value="Upload binary file">
</form>
<?php
$dbdatabase = "cbir";
$config_carname = "Content Based Image Retrieval";
$config_author = "Copyright &copy; 2013 shahd & arwa . All Rights Reserved.<br>      Designed by shahd & arwa";
$config_basedir = "http://localhost/cbir/";
$SCRIPT_NAME="process.php"; 
if(isset($_POST['upload']))
{
  //get file information --step1
  $fileName = $_FILES['userfile']['name'];
  $tmpName = $_FILES['userfile']['tmp_name'];
  $fileSize = $_FILES['userfile']['size'];
  //get file content -- step1
  $fp = fopen($tmpName, 'r');
  $content = fread($fp, $fileSize);
  $content = addslashes($content);
  fclose($fp);
  $link = mysql_connect("localhost", "root", "root");
  mysql_select_db($dbdatabase, $link)or die("<b>Unable to specified database</b>");

  $reds = array_fill(0, 256, 0);
  $blues = array_fill(0, 256, 0);
  $greens = array_fill(0, 256, 0);
  $info = getimagesize($_FILES['image']['tmp_name']);
  $width = $info[0];
  $height = $info[1];
  $totalpixels = $width * $height;
  $img = imagecreatefromjpeg($_FILES['image']['tmp_name']);
  if ($img) {
    for ($i = 0; $i < $height; $i++) {
      for ($j = 0; $j < $width; $j++) {
        $rgb = imagecolorat($img, $j, $i);
        $r = ($rgb >> 16) & 0xFF;
        $g = ($rgb >> 8) & 0xFF;
        $b = $rgb & 0xFF;
        if (!isset($reds[$r])) {
          $reds[$r] = 0;
        }
        if (!isset($greens[$r])) {
          $greens[$r] = 0;
        }
        if (!isset($blues[$r])) {
          $blues[$r] = 0;
        }
        // Add counts to our histogram arrays for each color.
        $reds[$r]++;
        $greens[$g]++;
        $blues[$b]++;
      }
    }
    $red_count=count($reds);
    $blue_count=count($blues);
    $green_count=count($greens);        }
    //insert into database --step3 
    $query = "INSERT INTO cbir (image, red_count,blue_count,green_count) VALUES    ('$content','$red_count','$blue_count','$green_count')";
    mysql_query($query) or die('Error, query failed');
    //return insert information to client
    $id= mysql_insert_id();
    echo "File<b> $fileName</b> uploaded as id= $id<br>";
  }
  ?>

您是否已将enctype="multipart/form-data"放入form标签中?

编辑

$_FILES['image']['tmp_name']中应该包含什么?

它不应该和$_FILES['userfile']['tmp_name']一样吗?

HTML代码中没有任何name="image"字段。

由于您创建了一个$tmpName变量,因此应该重用它,而不是每次调用$_FILES['image']['tmp_name']