【问题标题】:How to copy a file from one directory to another using PHP?如何使用 PHP 将文件从一个目录复制到另一个目录?
【发布时间】:2011-08-11 23:32:10
【问题描述】:

假设我在foo 目录和bar 中有一个文件test.php。如何使用PHPbar/test.php 替换为foo/test.php?我使用的是 Windows XP,跨平台解决方案会很棒,但首选 Windows。

【问题讨论】:

    标签: php file file-io copy filesystems


    【解决方案1】:

    您可以使用copy() 函数:

    // Will copy foo/test.php to bar/test.php
    // overwritting it if necessary
    copy('foo/test.php', 'bar/test.php');
    


    引用其手册页中的几个相关句子:

    将文件源复制到 目的地

    如果目的地 文件已经存在,它将是 覆盖。

    【讨论】:

    • copy( 'foo/test.php', 'bar/test.php' ) 是否创建 bar 目录(如果它尚不存在)?
    • 不@henrywright,它不会自己创建目录。您必须手动完成。 check it on php manual
    • 我没有意识到这是一件事......感谢分享。
    【解决方案2】:

    您可以使用rename() 函数:

    rename('foo/test.php', 'bar/test.php');
    

    然而,这将移动文件不复制

    【讨论】:

    • 不知道为什么把这个函数命名为rename and note move之类的
    • @themis 我也希望他们将函数命名为 move。如果一个人有一点 linux 暴露,那将是直观的。
    • @themis 因为rename('foo/test1.php', 'foo/test2.php'); ;)
    【解决方案3】:

    copy 会这样做。请查看php-manual。简单的 Google 搜索应该可以回答您的最后两个问题;)

    【讨论】:

      【解决方案4】:

      你可以复制过去,这对你有帮助

      <?php
      $file = '/test1/example.txt';
      $newfile = '/test2/example.txt';
      if(!copy($file,$newfile)){
          echo "failed to copy $file";
      }
      else{
          echo "copied $file into $newfile\n";
      }
      ?>
      

      【讨论】:

        【解决方案5】:

        使用 PHP 将所有文件从一个文件夹复制到另一个文件夹的最佳方法

        <?php
        $src = "/home/www/example.com/source/folders/123456";  // source folder or file
        $dest = "/home/www/example.com/test/123456";   // destination folder or file        
        
        shell_exec("cp -r $src $dest");
        
        echo "<H2>Copy files completed!</H2>"; //output when done
        ?>
        

        【讨论】:

          【解决方案6】:

          大家好,还想补充一下如何使用动态复制和粘贴进行复制。

          假设我们不知道用户将创建的实际文件夹,但我们知道在该文件夹中我们需要将文件复制到,以激活删除、更新、查看等功能。

          你可以使用这样的东西......我在我目前正忙于的一个复杂项目中使用了这段代码。我只是自己构建它,因为我在互联网上得到的所有答案都给了我一个错误。

              $dirPath1 = "users/$uniqueID"; #creating main folder and where $uniqueID will be called by a database when a user login.
              $result = mkdir($dirPath1, 0755);
                      $dirPath2 = "users/$uniqueID/profile"; #sub folder
                      $result = mkdir($dirPath2, 0755);
                          $dirPath3 = "users/$uniqueID/images"; #sub folder 
                          $result = mkdir($dirPath3, 0755);
                              $dirPath4 = "users/$uniqueID/uploads";#sub folder
                              $result = mkdir($dirPath4, 0755);
                              @copy('blank/dashboard.php', 'users/'.$uniqueID.'/dashboard.php');#from blank folder to dynamic user created folder
                              @copy('blank/views.php', 'users/'.$uniqueID.'/views.php'); #from blank folder to dynamic user created folder
                              @copy('blank/upload.php', 'users/'.$uniqueID.'/upload.php'); #from blank folder to dynamic user created folder
                              @copy('blank/delete.php', 'users/'.$uniqueID.'/delete.php'); #from blank folder to dynamic user created folder
          

          我认为 facebook 或 twitter 使用类似的东西来构建每个新的用户仪表板动态......

          【讨论】:

            【解决方案7】:

            你可以同时使用 rename() 和 copy()。

            如果我不再需要源文件保留在其位置,我倾向于使用重命名。

            【讨论】:

              【解决方案8】:
              <?php  
                
              // Copy the file from /user/desktop/geek.txt  
              // to user/Downloads/geeksforgeeks.txt' 
              // directory 
                
              // Store the path of source file 
              $source = '/user/Desktop/geek.txt';  
                
              // Store the path of destination file 
              $destination = 'user/Downloads/geeksforgeeks.txt';  
                
              // Copy the file from /user/desktop/geek.txt  
              // to user/Downloads/geeksforgeeks.txt' 
              // directory 
              if( !copy($source, $destination) ) {  
                  echo "File can't be copied! \n";  
              }  
              else {  
                  echo "File has been copied! \n";  
              }  
                
              ?>  
              

              【讨论】:

                【解决方案9】:

                PHP 的copy() 函数实际上只有在目标文件有要覆盖的文件时才有效。例如,您正在将文件 A 复制到文件 B,copy() 函数的工作方式如下:

                $fileA = "foo/fileA.txt";
                $fileB = "bar/fileA.txt";
                copy($fileA, $fileB);
                

                所以copy() 需要在目标路径中有一个文件,实际上目标路径也应该包含该文件名,否则它将出错并且无法工作。

                但是,如果您在目的地没有任何文件可以很好地覆盖而不是先创建一个具有该名称的文件,那么您将编写类似这样的内容;

                $source = "foo/fileA.txt";
                $destination = "bar/"; // note how the destination has no file.
                $newFile = "somefile.txt";
                touch($destination . $newFile);
                // then do the copy part
                copy($source, $destination.$newFile);
                

                我遇到了同样的问题,并提出了在目标位置创建文件以供复制功能覆盖的解决方案。

                【讨论】:

                  猜你喜欢
                  • 2012-02-15
                  • 1970-01-01
                  • 1970-01-01
                  • 2017-11-18
                  • 2013-06-01
                  • 2020-06-22
                  • 2014-08-12
                  • 2013-10-24
                  相关资源
                  最近更新 更多