2012-05-11 13 views
0

私はギャラリーに画像をアップロードするためのコードスニペットを持っています。すべてがコードの最後の部分に達するまでは、すべて動作しているようです。画像を保存できませんPHP

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)。これは、ディレクトリ'../images/gallery/'.$pubID.'/thumb/'が存在しないと考えます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 
+0

私はあなたがこのコードのコードインジェクションの脆弱性に気付いていると思いますか? – symcbean

答えて

1

:ここ

は、エラーコードの行84があることです。 mkdir('../images/gallery/'.$pubID.'/thumb/', 0775, true)を使用して、何が起こるかを確認してください。これにより、パスに沿って最後にthumbまで書き込み可能なディレクトリが作成されます。

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  

} 
関連する問題