【问题标题】:Copy images from one folder to another将图像从一个文件夹复制到另一个文件夹
【发布时间】:2013-01-01 07:56:19
【问题描述】:

我的 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";
                }
                   }
            }
        }
?>

【问题讨论】:

  • 确保为目标文件夹设置了正确的权限

标签: php


【解决方案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

【讨论】:

    【解决方案2】:

    在你的代码中替换这一行,这肯定会工作。

    if(copy("../images/".$readFile, "../img/".$readFile))
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:
        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));
          }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-07-30
          • 1970-01-01
          • 2014-09-17
          • 1970-01-01
          • 2023-01-14
          • 1970-01-01
          • 2011-08-22
          • 1970-01-01
          相关资源
          最近更新 更多