2

我的问题与类似,但我被困在最后一点!

以下代码采用 HTML 表单发送的文件,并根据两个预定义值检查其尺寸,$maxwidth以及$maxheight. 如果它太宽,它会调整图像的大小。

我接下来要做的就是将新调整大小的图像重新分配给原始变量$tmpName,以便脚本的其余部分可以处理它。

以下代码返回这些错误:

Warning: fopen() expects parameter 1 to be string, resource given in /var/www/vhosts/galleryimaginem.com/httpdocs/img/upload.php on line 43

Warning: filesize() [function.filesize]: stat failed for Resource id #4 in /var/www/vhosts/galleryimaginem.com/httpdocs/img/upload.php on line 44

Warning: fread(): supplied argument is not a valid stream resource in /var/www/vhosts/galleryimaginem.com/httpdocs/img/upload.php on line 44

Warning: fclose(): supplied argument is not a valid stream resource in /var/www/vhosts/galleryimaginem.com/httpdocs/img/upload.php on line 46

我想我很接近得到它,但有人可以帮忙吗?谢谢。

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

      // Temporary file name stored on the server
      $tmpName  = $_FILES['image']['tmp_name'];  

            list($width,$height)=getimagesize($tmpName);

            if ($width>$height && $width>$maxwidth) {
                $newheight=($height/$width)*$maxwidth;
                $newwidth=$maxwidth;
                $imageResized = imagecreatetruecolor($newwidth, $newheight);
                $imageTmp     = imagecreatefromjpeg ($tmpName);
                imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
                $tmpName=$imageResized;

                // My problem lies somewhere here ^^^^
            }

      // Read the file 
      $fp      = fopen($tmpName, 'r');
      $data = fread($fp, filesize($tmpName));
      $data = addslashes($data);
      fclose($fp);


      // Create the query and insert
      // into our database.
      $query = "INSERT INTO tblPrints ";
      $query .= "(title,full_image) VALUES ('IMG-TEST','$data')";
      $results = mysql_query($query, $link);

      // Print results
      print "Thank you, your file has been uploaded.";

}
else {
   print "No image selected/uploaded";
}
4

1 回答 1

0

第 6-17 行将文件名替换为图像数据本身——这就是

Warning: fopen() expects parameter 1 to be string, resource given

所以在接下来的几行中,

  // Read the file 
  $fp      = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));
  $data = addslashes($data);

你不需要阅读文件。如果条件过程已经执行,你只需要

$data = $tmpname // sounds strange, remember: $tmpname is not the name any more
于 2013-05-15T15:42:02.173 回答