如何使用PHP将文件从一个目录复制到另一个目录?

179
说我有一个文件test.phpfoo目录和bar目录中。 我如何使用PHPbar/test.php替换为foo/test.php?我在Windows XP上,跨平台解决方案很好,但更喜欢Windows。
10个回答

329
你可以使用copy()函数:
// Will copy foo/test.php to bar/test.php
// overwritting it if necessary
copy('foo/test.php', 'bar/test.php');

从手册页面中引用几个相关的句子:
复制文件源到目标文件。如果目标文件已经存在,将会被覆盖。

12
copy( 'foo/test.php', 'bar/test.php' ) 如果bar目录不存在,会创建该目录吗? - henrywright
4
不,@henrywright,它本身不会创建目录。您需要手动创建目录。在php手册上查看 - Haseeb Zulfiqar
1
我没有意识到这是一件事...谢谢分享。 - Barry

24

您可以使用rename()函数:

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

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


23
我想知道为什么他们把这个函数命名为“rename”,而不是“move”或其他什么名称。 - themhz
@themis 我也希望他们把这个函数命名为“move”。如果有一点 Linux 经验的话,这将是直观的。 - Fr0zenFyr
7
因为 rename('foo/test1.php', 'foo/test2.php'); 所以提到了 @themis ;) - Anand Singh

14

复制功能可以实现此操作。请查看PHP手册。简单的谷歌搜索应该能够回答您的最后两个问题 ;)


11

您可以复制并粘贴这个:

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

7

使用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
?>

1
这不是一个好的例子,因为大多数Web服务器出于安全考虑已禁用了shell命令。 - TGates

1

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);

我曾经遇到同样的问题,并采用了这个解决方案:在目标位置创建一个文件,以便复制函数可以覆盖它。


1

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

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

您可以使用类似于以下代码... 我在我目前忙碌的一个复杂项目中使用了这段代码。我自己构建它,因为我在互联网上得到的所有答案都给我一个错误。

    $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使用类似的方法来构建每个新用户的动态仪表板....

0

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

如果我不再需要源文件保留在原位置,我倾向于使用rename()函数。


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

0
<?php

$file 'example.txt';
$newfile 'example2.txt';

if (!copy($file$newfile)) {
    echo "failed to copy $file...\n";
}
?>

这并没有回答问题。一旦您拥有足够的声望,您将能够评论任何帖子;相反,提供不需要询问者澄清的答案。- 来自审核 - Semih Arslanoglu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接