0

我的管理文件夹中有一个名为“repository”的文件夹。该文件夹包含 2 个文件:index.html 和 content.php。当用户创建一个新页面时,php会创建一个用户指定的新文件夹,然后需要将这两个文件复制到该文件夹​​中,同时将它们留在存储库中。

复制(文件,目的地)不起作用。rename(file,dest) 将文件移动到新文件夹,但我在存储库中丢失了它们。

如何将一个文件夹中的文件复制到新文件夹中而不丢失原始文件夹中的文件?

$dest = '../'.$menuLocation.'/'.$pageName; 
$file1= "repository/index.html"; 
$file2= "repository/content.php"; 
mkdir($dest,0777); 
rename($file1,$dest.'/index.html'); 
rename($file2,$dest.'/content.php');

$menuLocation 和 $pageName 由用户提供。文件在那里,file_exists 返回 true。此外,该目录的创建没有问题。rename() 也可以,我只是丢失了存储库中的文件。

4

2 回答 2

1

对于任何寻找解决方案的人:

在 php 中使用 copy() 时,还需要包含文件名。

copy(orginalfile,destination_with_filename);

例如:

错误的:

copy('/temp/sports/basketball.jpg','/images/sports/')

正确的:

copy('/temp/sports/basketball.jpg','/images/sports/basketball.jpg')
于 2010-03-11T21:13:23.127 回答
0

使用复制()。确保您捕获了返回值,以便您的程序知道它是否有效。还要检查文件和目录的权限,以确认执行 PHP 脚本的用户能够在您指定的位置创建新文件。您可能希望在目录上使用 is_writable() 来确认这些假设。

于 2010-03-11T17:56:06.063 回答