1

I want to copy selected image file one folder into another folder. Image selection process is dynamically. i am using below php code for copy process.

//$cnt is count of selected images

for($i=0;$i<$cnt;$i++)
    {
    $img = $sel['album_images']; //name of file to be copied
                //read the file
                $fp = fopen('uploads/members/album/'.$_SESSION['ses_userid'].'/'.$img, 'r') or die("Could not contact $img");
                $page_contents = "";
                while ($new_text = fread($fp, 100)) 
                {               
                $page_contents .= $new_text;
                }
                chdir("uploads/products/design/"); //This moves you from the current directory user to the images directory in the new user's directory
                $newfile = "product-".strtotime('now').".png"; //name of your new file
                //create new file and write what you read from old file into it
                $fd = fopen($newfile, 'w');
                fwrite($fd, $page_contents);
                fclose ($fd);
}

The above code is run perfectly for if the selected image count is 1.but it produce the error if the selected image count above 1.

Error is : 

Warning: fopen(uploads/members/album/72/full_e5829jellyfish.jpg) [function.fopen]: failed to open stream: No such file or directory in D:\wamp\www\myprint\photoprint_step2.php on line 51
    Could not contact full_e5829jellyfish.jpg

Note : If the selected image count is 2 then the first image will be copied successfully , next (2nd image) the error will be produced. What happened?

sabari k
  • 140
  • 6
  • Possible duplicate of [PHP - Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Sep 22 '16 at 07:54

1 Answers1

1

I assume that your for-cycle causes the errors, as you change your directory just once (and not change it back) and in the second and later iterations, you don't.

I wouldn't use chdir function, for cleanliness and readability of the code, it's better to use full paths.

$newfile = "uploads/products/design/product-".strtotime('now').".png";

Be aware of using strtotime('now') as the only indentificator of your image file, as many images could be saved within one second, and that can be your case. That's why your image has the same name in the second iteration, it saves the content of the second image, but replaces the previous one.

Try to put some random constant in it, like this

$newfile = "uploads/products/design/product-".strtotime('now')."-".rand(1000,9999).".png";
pedrouan
  • 12,762
  • 3
  • 58
  • 74
  • Now i check the code without chdir function ,error doesn't comes. But second image only will be inserted . First image doesn't inserted. I want insert all of my selected image. – sabari k Sep 17 '16 at 08:54
  • Because the directory is same for all images. So only i didn't change my directory. – sabari k Sep 17 '16 at 08:58
  • So your first error has been cleared and I updated my answer just a seconds ago, so try to check it and please make an appropriate action, if your are fine with my help. – pedrouan Sep 17 '16 at 09:06
  • You are correct , second image will be overwrite the first image.Now its work fine.Thanks for your support dude. – sabari k Sep 17 '16 at 09:21
  • Not only is it necessary to use full paths, they should also be absolute. You can read explanations and solutions here : http://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory – Vic Seedoubleyew Sep 22 '16 at 07:55