【问题标题】:How do I copy files repository to new folder with PHP如何使用 PHP 将文件存储库复制到新文件夹
【发布时间】:2011-01-26 11:53:57
【问题描述】:

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

copy(file,dest) 不起作用。 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() 也可以,我只是丢失了存储库中的文件。

【问题讨论】:

  • 复制应该可以。请发布您的代码。
  • $dest = '../'.$menuLocation.'/'.$pageName; $file1="repository/index.html"; $file2="repository/content.php"; mkdir($dest,0777);重命名($file1,$dest.'/index.html');重命名($file2,$dest.'/content.php'); $menuLocation 和 $pageName 由用户提供。如果文件在那里,file_exists 返回 true。
  • 感谢戈登的编辑。直到我添加评论后,我才注意到这一点。今天很忙,脑子很乱。
  • 请将rename 更改为copy 并将error_reporting(-1); 放在您的脚本顶部。它有任何错误吗?另外,请将echo __DIR__; 放入脚本中,然后将其输出给我们。
  • 我只需要添加 .'/index.html' 和 .'/content.php'。谢谢!

标签: php copy rename dir


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    对于任何寻求解决此问题的人:

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

    copy(orginalfile,destination_with_filename);
    

    例如:

    错误:

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

    正确:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-13
      • 1970-01-01
      • 2023-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      相关资源
      最近更新 更多