2

我的 Web 应用程序存储在 XAMPP/htdocs/projectname/ 目录中。我在上面的目录中有图像(源)和 img(目标)文件夹。我正在编写以下代码行以将图像从一个文件夹复制到另一个文件夹。但我收到以下警告:(警告:复制(资源 id #3/image1.jpg):无法打开流:C:\xampp\htdocs 中没有这样的文件或目录) 并且图像不会复制到目的地。

<?php
        $src = opendir('../images/');
    $dest = opendir('../img/');
    while($readFile = readdir($src)){
            if($readFile != '.' && $readFile != '..'){
                 if(!file_exists($readFile)){
                if(copy($src.$readFile, $dest.$readFile)){
                echo "Copy file";
            }else{
                    echo "Canot Copy file";
                }
                   }
            }
        }
?>
4

4 回答 4

1

只是一个猜测(对不起),但我不相信你可以使用$src = opendir(...)$src.$readFile喜欢那样。尝试这样做:

$srcPath = '../images/';
$destPath = '../img/';  

$srcDir = opendir($srcPath);
while($readFile = readdir($srcDir))
{
    if($readFile != '.' && $readFile != '..')
    {
        /* this check doesn't really make sense to me,
           you might want !file_exists($destPath . $readFile) */
        if (!file_exists($readFile)) 
        {
            if(copy($srcPath . $readFile, $destPath . $readFile))
            {
                echo "Copy file";
            }
            else
            {
                echo "Canot Copy file";
            }
        }
    }
}

closedir($srcDir); // good idea to always close your handles
于 2013-01-01T08:05:14.277 回答
1

在您的代码中替换这一行,这肯定会起作用。

if(copy("../images/".$readFile, "../img/".$readFile))
于 2013-01-01T08:21:41.533 回答
0

你给出的路径错误,如果你的文件路径说 script.php 是“XAMPP/htdocs/projectname/script.php”并且图像和 img 都在“projectname”文件夹中,那么你应该为 $srcPath 和 $destPath 使用以下值,将它们的值更改为

$srcPath = 'images/';
$destPath = 'img/';
于 2013-01-01T08:19:48.703 回答
0
public function getImage()
  {

    $Path='image/'; //complete image directory path
    $destPath = '/edit_image/';
    // makes new folder, if not exists.
    if(!file_exists($destPath) || file_exists($destPath)) 
    {
        rmdir($destPath);
        mkdir($destPath, 0777);
    }

    $imageName='abc.jpg';
    $Path=$Path.$imageName;
    $dest=$destPath.$imageName;
    if(copy($Path, $dest));
  }
于 2016-08-10T13:20:37.887 回答