错误:"无法打开流。" PHP图像上传脚本

时间:2014-10-11 20:43:37

标签: php apache

剧本:

<?php
if (isset($_POST['submit'])) {
    $j = 0; //Variable for indexing uploaded image 

    $target_path = $_SERVER['DOCUMENT_ROOT'] . "/gallerysite/multiple_image_upload/uploads/"; //Declaring Path for uploaded images

    for ($i = 0; $i < count($_FILES['file']['name']); $i++) {//loop to get individual element from the array

        $validextensions = array("jpeg", "jpg", "png");  //Extensions which are allowed
        $ext = explode('.', basename($_FILES['file']['name'][$i]));//explode file name from dot(.) 
        $file_extension = end($ext); //store extensions in the variable

        $new_image_name = md5(uniqid()) . "." . $ext[count($ext) - 1];
        $target_path = $target_path . $new_image_name;//set the target path with a new name of image
        $j = $j + 1;//increment the number of uploaded images according to the files in array       

      if (($_FILES["file"]["size"][$i] < 100000) //Approx. 100kb files can be uploaded.
                && in_array($file_extension, $validextensions)) {
            if (move_uploaded_file($_FILES['file']['tmp_name'][$i], $target_path)) {//if file moved to uploads folder
                echo $j. ').<span id="noerror">Image uploaded successfully!.</span><br/><br/>';



                for ($i = 0; $i < count($_FILES['file']['name']); $i++) {
                    $tqs = "INSERT INTO images (`original_image_name`, `image_file`, `date_created`) VALUES ('" . $_FILES['file']['name'][$i] . "', '" . $new_image_name  . "', now())";
                    $tqr = mysqli_query($dbc, $tqs);
                }



                // To create the thumbnails.
                function make_thumb($src, $dest, $desired_width) {

                    /* read the source image */
                    $source_image = imagecreatefromjpeg($src);
                    $width = imagesx($source_image);
                    $height = imagesy($source_image);

                    /* find the "desired height" of this thumbnail, relative to the desired width  */
                    $desired_height = floor($height * ($desired_width / $width));

                    /* create a new, "virtual" image */
                    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

                    /* copy source image at a resized size */
                    imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);

                    /* create the physical thumbnail image to its destination */
                    imagejpeg($virtual_image, $dest);
                }


                $src = $target_path;
                $dest = $_SERVER['DOCUMENT_ROOT'] . "/gallerysite/multiple_image_upload/thumbs/";
                $desired_width = 100;

                make_thumb($src, $dest, $desired_width);




            } else {//if file was not moved.
                echo $j. ').<span id="error">please try again!.</span><br/><br/>';
            }
        } else {//if file size and file type was incorrect.
            echo $j. ').<span id="error">***Invalid file Size or Type***</span><br/><br/>';
        }
    }

}


?>

错误讯息:

Warning: imagejpeg(C:/xampp/htdocs/gallerysite/multiple_image_upload/thumbs/): failed to open stream: No such file or directory in C:\xampp\htdocs\gallerysite\multiple_image_upload\upload.php on line 49

“第49行”:

/* create the physical thumbnail image to its destination */
                    imagejpeg($virtual_image, $dest);
                }

该脚本还使用javascript进行多个图片上传。

脚本本身工作正常。

我在这里有这个部分,我没有收到此错误消息,这意味着与我收到错误消息的部分相比,这部分工作正常:

 $target_path = $_SERVER['DOCUMENT_ROOT'] . "/gallerysite/multiple_image_upload/uploads/"; //Declaring Path for uploaded images

这是我收到错误消息的部分(再次,这是比较)。该点位于脚本创建缩略图的位置:

$dest = $_SERVER['DOCUMENT_ROOT'] . "/gallerysite/multiple_image_upload/thumbs/";

文件夹“thumbs”确实存在。虽然,在Windows中它被设置为“只读”,当我尝试取消选中属性中的“只读”选项时,它会将其自身重新设置为“只读”。

有关如何解决此问题的任何建议?

0 个答案:

没有答案