0

我的网站上有一个上传系统,它检测图像是否大于某个尺寸,如果是,则使用以下代码将其调整为该尺寸:

<?php
if($count >= "1"){
    echo "<h3>The Name : ".$_POST['textfield']. " already exists. </h3>";
    echo "<h3>Please choose a unique name for this photo</h3>";
}else{  
if (($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg"))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_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 "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";



    if (file_exists("uploads/images/photography/" . $_FILES["file"]["name"]))
      {
      echo "<h3>".$_FILES["file"]["name"] . " already exists. </h3>";
      }
    else
      {

// Temporary upload image name 
$original_image = $_FILES["file"]["tmp_name"]; 

// Get the image dimensions 
$size=getimagesize( $original_image ); 

if((($size[0])<1600) || (($size[1])<1600)){

$new_image = $_FILES["file"]["name"]; 

move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/images/photography/" . $_FILES["file"]["name"]);


echo "File uploaded<br><br>"; 

echo "<img src=\"watermark.php?path=".$new_image."\" width=\"900\"><br><br>"; 

}else{

// Name to save the image as - in this case the same as the original 
$new_image = $_FILES["file"]["name"]; 

// Maximum image width 
$max_width = "1600"; 

// Maximum image height 
$max_height = "1600"; 

// Resize the image and save 
exec("convert -size {$size[0]}x{$size[1]} $original_image -thumbnail $max_widthx$max_height $new_image"); 

echo "File uploaded<br><br>"; 

echo "<img src=\"watermark.php?path=".$new_image."\" width=\"900\"><br><br>"; 

$copy = copy($new_image, "uploads/images/photography/".$new_image);
$unlink = unlink($new_image);

}

      echo "Stored in: " . "uploads/images/photography/" . $_FILES["file"]["name"];

      $date = date("d/m/y");

      $query  = mysql_query ('INSERT INTO  `ap_photos_list` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'")');

      $sizes = mysql_query("SELECT * FROM ap_sizes ORDER BY id ASC");

      while($row = mysql_fetch_assoc($sizes)){
            $query  = mysql_query ('INSERT INTO  `ap_photos` ( `photo_id` ,  `category_id` ,  `subcategory_id` ,  `photo_name` ,  `photo_size` ,  `upload_date` ,  `filename` , `price` , `size` ) VALUES ("",  "'.$_REQUEST['category'].'",  "'.$_REQUEST['sub_category'].'",  "'.$_POST['textfield'].'",  "'.($_FILES["file"]["size"] / 1024).'KB",  "'.$date.'",  "'. $_FILES["file"]["name"].'" , "'.$row['price'].'" , \''.$row['size'].'\')');
      }
      }
    }
  }
else
  {
  echo "<h3>Invalid file</h3>";
  }
}
?>

但是,会出现以下错误或问题之一:

  • 我收到以下错误:

    1. Warning: copy(DSC00103.JPG) [function.copy]: failed to open stream: No such file or directory in /homepages/19/d372249701/htdocs/ap-photo/admin/new_photo.php on line 220
    2. Warning: unlink(DSC00103.JPG) [function.unlink]: No such file or directory in /homepages/19/d372249701/htdocs/ap-photo/admin/new_photo.php on line 221
  • 或者它只是不传输图像

这两个结果都表明我上传的图像不存在?!?

任何解决方案的建议将不胜感激。

更新 1

我出于版权目的保护文件的 .htaccess 文件:

RewriteEngine On
RewriteCond %{REQUEST_URI} $
RewriteRule \.(gif|jpg|png|JPG)$ - [L,F]
ErrorDocument 403 /admin/uploads/error-docs/403.shtml

更新 2

我浏览了代码并确定问题出在:

  • exec("convert -size {$size[0]}x{$size[1]} $original_image -thumbnail $max_widthx$max_height $new_image");

  • $copy = copy($new_image, "/homepages/19/d372249701/htdocs/ap-photo/admin/uploads/images/photography/".$new_image); $unlink = unlink($new_image);

我怀疑是回声。

4

3 回答 3

0

有几件事要检查:

  • 您尝试保存的目录是否可写?(上传/图片/摄影)
  • 图像是否比您的 PHP 设置允许的更大?(通常在 php.ini 中,upload_max_filesize)

编辑

您需要“复制”脚本中上传位置的绝对路径,该路径以类似的内容开头

/homepages/19/d372249701/htdocs
于 2012-05-17T15:38:21.710 回答
0

似乎您的 upload_max_filesize 设置为默认的 2MB。请使用以下内容设置 .htaccess:

php_value upload_max_filesize    110M
php_value post_max_size          120M

有关更多信息,请参阅此处

于 2012-05-17T15:39:26.617 回答
0

我会尝试:

1/ 在上传过程中显示任何带有 php 错误的错误:http: //php.net/manual/en/features.file-upload.errors.php

2/ 在不同阶段用 echo 显示变量以查看它们包含的内容。

例如

$original_image = $_FILES["file"]["tmp_name"];
echo "original image = $original_image<br>";

3/ 隔离代码的各个部分并检查每个步骤是否有效。

编辑答案:

老实说,我没有阅读 Imagemagick 代码!

-size {$size[0]}x{$size[1]} is not required

我这样编写代码,这样可以更轻松地检查 Imagemagick 代码,因为您可以添加回显以显示 $cmd 的内容

$cmd = "$original_image -thumbnail $max_widthx$max_height ";
exec(" convert $cmd $new_image"); 

Imagemagick 不适用于错误检查,但您可以添加以下内容:

$array=array(); 
echo "<pre>"; 
$cmd = "$original_image -thumbnail $max_widthx$max_height ";
exec(" convert $cmd $new_image 2>&1", $array);  
echo "<br>".print_r($array)."<br></pre>";

$new_image 只需要是带有图像名称的保存位置的相对路径。

于 2012-05-17T16:25:57.110 回答