0

我有一段代码用于将图像上传到我的画廊。一切似乎都有效,直到它到达代码的最后一部分。

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
     $pubID = $_POST['pubID'];
     $size = 260; // the thumbnail height
     $filedir = '../images/gallery/'.$pubID.'/'; // the directory for the original image
     $thumbdir = '../images/gallery/'.$pubID.'/thumb/'; // the directory for the thumbnail image
     $maxfile = '2000000';
     $mode = '0777';
     $userfile_name = $_FILES['Gallimage']['name'];
     $userfile_tmp = $_FILES['Gallimage']['tmp_name'];
     $userfile_size = $_FILES['Gallimage']['size'];
     $userfile_type = $_FILES['Gallimage']['type'];
     if (isset($_FILES['Gallimage']['name']))
     {
         $prod_img = $filedir.$userfile_name;
         $prod_img_thumb = $thumbdir.$userfile_name;
         move_uploaded_file($userfile_tmp, $prod_img);
         chmod ($prod_img, octdec($mode));
         $sizes = getimagesize($prod_img);
         $aspect_ratio = $sizes[1]/$sizes[0]; 
         if ($sizes[1] <= $size)
         {
             $new_width = $sizes[0];
             $new_height = $sizes[1];
         }else{
             $new_height = $size;
             $new_width = abs($new_height/$aspect_ratio);
         }
         $destimg=ImageCreateTrueColor($new_width,$new_height)
             or die('Problem In Creating image');
         $srcimg=ImageCreateFromJPEG($prod_img)
             or die('Problem In opening Source Image');
         if(function_exists('imagecopyresampled'))
         {
             imagecopyresampled($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
             or die('Problem In resizing');
         }else{
             Imagecopyresized($destimg,$srcimg,0,0,0,0,$new_width,$new_height,ImageSX($srcimg),ImageSY($srcimg))
             or die('Problem In resizing');
         }
         ImageJPEG($destimg,$prod_img_thumb,90)
             or die('Problem In saving');
         imagedestroy($destimg);
     }

错误出现在这一行:ImageJPEG($destimg,$prod_img_thumb,90)。

这是第 84 行的错误代码:ImageJPEG($destimg,$prod_img_thumb,90)

Warning: imagejpeg() [function.imagejpeg]: Unable to open '../images/gallery/264/thumb/Hair-Salon-1.jpg' for writing: No such file or directory in /home/www/public_html/console/gallery.php on line 84. Problem In saving
4

2 回答 2

1

可以想象,该目录'../images/gallery/'.$pubID.'/thumb/'不存在。尝试使用mkdir('../images/gallery/'.$pubID.'/thumb/', 0775, true),看看会发生什么。这应该沿着路径创建可写目录,直到thumb最后。

于 2012-05-11T12:15:09.293 回答
0

试试这个代码,进行更多的错误检查、一些更正和一些一般性的改进。全部评论,如果您不明白任何内容,请询问:

// Try not to over-use parenthesis, it makes code less readable. You only need them to group conditions.
if (isset($_POST["MM_insert"]) && $_POST["MM_insert"] == "form1") {

  // Pass through basename() for sanitization
  $pubID = basename($_POST['pubID']);

  $size = 260; // the thumbnail height
  $filedir = '../images/gallery/'.$pubID.'/'; // the directory for the original image
  $thumbdir = '../images/gallery/'.$pubID.'/thumb/'; // the directory for the thumbnail image
  $maxfile = '2000000';

  // PHP understands proper octal representations - no need to turn it into a string
  $mode = 0777;

  if (isset($_FILES['Gallimage']['name'])) {

    // Get upload info and create full paths
    $userfile_name = $_FILES['Gallimage']['name'];
    $userfile_tmp = $_FILES['Gallimage']['tmp_name'];
    $userfile_size = $_FILES['Gallimage']['size'];
    $userfile_type = $_FILES['Gallimage']['type'];
    $prod_img = $filedir.$userfile_name;
    $prod_img_thumb = $thumbdir.$userfile_name;

    // Create directories if they don't exist - this is the crux of your problem
    if (!is_dir($filedir)) {
      mkdir($filedir, $mode, TRUE)
        or die('Unable to create storage directory');
    }
    if (!is_dir($thumbdir)) {
      mkdir($thumbdir, $mode, TRUE))
        or die('Unable to create thumb directory');
    }

    // Move it to the correct location and set permissions
    move_uploaded_file($userfile_tmp, $prod_img)
      or die('Unable to move file to main storage directory');
    chmod($prod_img, $mode)
      or die('Unable to set permissions of file');

    // Get info about the image
    $sizes = getimagesize($prod_img);
    $aspect_ratio = $sizes[1] / $sizes[0]; 
    if ($sizes[1] <= $size) {
      $new_width = $sizes[0];
      $new_height = $sizes[1];
    } else {
      $new_height = $size;
      $new_width = abs($new_height / $aspect_ratio);
    }

    // Create image resources
    $destimg = imagecreatetruecolor($new_width, $new_height)
      or die('Problem in creating image');
    $srcimg = imagecreatefromjpeg($prod_img)
      or die('Problem in opening source Image');

    // Manipulate the image
    if (function_exists('imagecopyresampled')) {
      imagecopyresampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, imagesx($srcimg), imagesy($srcimg))
        or die('Problem in resizing');
    } else {
      imagecopyresized($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, imagesx($srcimg), imagesy($srcimg))
        or die('Problem in resizing');
    }

    // Save the thumbnail and destroy the resources
    imagejpeg($destimg, $prod_img_thumb, 90)
      or die('Problem in saving');
    imagedestroy($destimg);

  }

  // More code here? If not, merge all of this into a single if block      

}
于 2012-05-11T12:35:17.307 回答