Reputation: 140
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?
Upvotes: 1
Views: 2145
Reputation: 12910
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";
Upvotes: 1