Jump to content

Error: "Failed to open stream." - Image Upload Script


glassfish
Go to solution Solved by mac_gyver,

Recommended Posts

The Script:

<?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/>';
        }
    }

}


?>

With this:

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

I get this error message:

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

With this:

$dest = "http://localhost/gallerysite/multiple_image_upload/thumbs/";

I get this error message:

Warning: imagejpeg(http://localhost/gallerysite/multiple_image_upload/thumbs/): failed to open stream: HTTP wrapper does not support writeable connections in C:\xampp\htdocs\gallerysite\multiple_image_upload\upload.php on line 49

When I try deleting the "thumbs" folder and then try to upload an image, then I get this error message:

Warning: imagejpeg(C:/xampp/htdocs/gallerysite/multiple_image_upload/thumbs/): failed to open stream: Invalid argument in C:\xampp\htdocs\gallerysite\multiple_image_upload\upload.php on line 49

The spot of line 49 is this, at the spot where the script creates the thumbnails:

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

Also, with this part right here I am not getting an error message, which means that this part works fine in comparison:

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

The script itself works fine. The script also uses javascript for multiple image upload.

 

I am using XAMPP. The folder "thumbs" is set to "read-only", when I try to uncheck the "read-only" option in the properties in Windows then it sets itself back again to "read-only". Then again, the script works fine when it comes to the "uploads" folder. This is mentioned in comparison to the "thumbs" folder.

 

Any suggestions on how to solve this?

 

EDIT:

 

I am using XAMPP. The folder "thumbs" is set to "read-only", when I try to uncheck the "read-only" option in the properties in Windows then it sets itself back again to "read-only".

I am wondering if I would have issue that the option sets itself back again with Linux?

Edited by glassfish
Link to comment
Share on other sites

  • Solution

you are not supply the file name to the imagejpeg() function. you are only supplying the path.

 

edit: your code is allowing jpg/jpeg and png files to be uploaded. the make_thumb() function would need to be able to work for both of those type of images.

 

also, one type of upload error, exceeding the post_max_size setting, will cause both the $_POST and $_FILES arrays to be empty. you must test if the upload worked before you can access any of the $_POST and $_FILES data.

 

another problem is you are defining the make_thumb() function inside of a loop. this will result in a fatal runtime error on the second pass through the loop. functions should be defined only once and never in side of loops.

 

and even another problem with your code is you are overwriting the $target_path inside the loop, so on the second pass through the loop, your code will be attempting to move the uploaded file to a jumbled up filename.

Link to comment
Share on other sites

you are not supply the file name to the imagejpeg() function. you are only supplying the path.

The file name is there too, as you see on this line:

 

and even another problem with your code is you are overwriting the $target_path inside the loop, so on the second pass through the loop, your code will be attempting to move the uploaded file to a jumbled up filename.

The script uses javascript for multiple image upload.(?)

 

When I try to move the "function definition" out of the loop I still get the same problem.

 

My question to this part of one error message would be:

 failed to open stream: Invalid argument

What type of reasons could  there be so that the argument is invalid?

 

 

Should I try "to force" unchecking the "read-only" option in Windows? Would I be able to uncheck the "read-only" option in Linux, for ex when using XAMPP and the folder is set "read-only"

Link to comment
Share on other sites

if you look at the first error message and look at your code leading up to the imagejpeg() function, you will see that you are NOT supplying a file name - 

 

Warning: imagejpeg(http://localhost/gallerysite/multiple_image_upload/thumbs/): <--- there is no file name here, only the path.... 

 

 

at this point, we cannot possibly help you because you not reading the error messages, nor are you even considering the information that someone took the time to give you in a reply.

Link to comment
Share on other sites

/* create the physical thumbnail image to its destination */

I did not think that part needs a file name, I thought $virtual_image contains the filename already, and that part you quoted is basically the destination. Or imagejpeg(); works differently than I thought, so basically at that first spot it is the resizing and at the second spot moving the file to the destination. Both spots with the file name? I will try it out in a minute.

 

EDIT:

Oops, I may have misunderstood the script (or also imagejpeg()) trying this has worked:

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

Thanks a lot!

Edited by glassfish
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.