简体   繁体   English

无法打开流:没有这样的文件或目录

[英]Failed to open stream: No such file or directory

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.我正在使用以下 php 代码进行复制过程。

//$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.如果所选图像计数为 1,则上述代码完美运行。但如果所选图像计数高于 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.注意:如果选择的图像计数为 2,则第一张图像将被成功复制,接下来(第二张图像)将产生错误。 What happened?发生了什么?

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.我假设您的for-cycle会导致错误,因为您只更改了一次目录(而不是将其改回),而在第二次和以后的迭代中,您不会。

I wouldn't use chdir function, for cleanliness and readability of the code, it's better to use full paths.我不会使用 chdir 函数,为了代码的简洁性和可读性,最好使用完整路径。

$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.请注意使用strtotime('now')作为图像文件的唯一标识符,因为可以在一秒钟内保存许多图像,这可能就是您的情况。 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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM