说我有一个文件
test.php
在foo
目录和bar
目录中。 我如何使用PHP
将bar/test.php
替换为foo/test.php
?我在Windows XP上,跨平台解决方案很好,但更喜欢Windows。test.php
在foo
目录和bar
目录中。 我如何使用PHP
将bar/test.php
替换为foo/test.php
?我在Windows XP上,跨平台解决方案很好,但更喜欢Windows。copy()
函数:// Will copy foo/test.php to bar/test.php
// overwritting it if necessary
copy('foo/test.php', 'bar/test.php');
rename('foo/test1.php', 'foo/test2.php');
所以提到了 @themis ;) - Anand Singh您可以复制并粘贴这个:
<?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";
}
?>
使用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
?>
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);
我曾经遇到同样的问题,并采用了这个解决方案:在目标位置创建一个文件,以便复制函数可以覆盖它。
大家好,我想补充一下如何使用动态复制和粘贴。
假设我们不知道用户将创建的实际文件夹,但我们知道在该文件夹中需要复制文件以激活某些功能,例如删除、更新、查看等。
您可以使用类似于以下代码... 我在我目前忙碌的一个复杂项目中使用了这段代码。我自己构建它,因为我在互联网上得到的所有答案都给我一个错误。
$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
你可以同时使用rename()和copy()函数。
如果我不再需要源文件保留在原位置,我倾向于使用rename()函数。
<?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";
}
?>
<?php
$file = 'example.txt';
$newfile = 'example2.txt';
if (!copy($file, $newfile)) {
echo "failed to copy $file...\n";
}
?>
copy( 'foo/test.php', 'bar/test.php' )
如果bar
目录不存在,会创建该目录吗? - henrywright