无法理解来自图片上传表单的这些错误

时间:2011-07-22 07:57:30

标签: php file-upload

我遇到了这样的错误:

imagecopyresampled()要求参数2为资源,C:\ xampp \ htdocs中给出的字符串file_put_contents():提供的资源不是C:\ xampp \ htdocs中的有效流资源

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 80000))
  {
if($_FILES["file"]["error"]>0)
    {
        echo "Error:".$_FILES["file"]["error"]."</br>";
    }
else
    {
        echo "Upload: ".$_FILES["file"]["name"]."</br>";
         echo "Type: " . $_FILES["file"]["type"] . "<br />";
          echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
          echo "Stored in: " . $_FILES["file"]["tmp_name"];
          $tmp=$_FILES["file"]["tmp_name"];

     if (file_exists("images/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
        $image = file_get_contents($tmp);
        $new_image = imagecreatetruecolor(200, 200);
        imagecopyresampled($new_image, $image, 0, 0, 0, 0, 200, 200, imagesx($image), imagesy($image));
        file_put_contents($image, $new_image);
        move_uploaded_file($_FILES["file"]["tmp_name"],
        "images/" . $_FILES["file"]["name"]);
        echo "Stored in: " . "images/" . $_FILES["file"]["name"];
      }
  }
  }
  else
  {
      echo"Invalid file";
  }
?>

1 个答案:

答案 0 :(得分:1)

更改此行:

$image = file_get_contents($tmp);

要:

$image = imagecreatefromstring(file_get_contents($tmp));

imagecopyresampled期望第二个参数是GD图像资源,而不是file_get_contents结果的字符串。使用imagecreatefromstring将图像的字符串表示形式转换为图像资源。