使用PHP将现有文件复制到新目录中


Copy existing file into new directory using PHP

不知道为什么我的代码不工作。我最终试图复制整个目录,但现在只复制一个文件,因为"while循环"函数仍然超出我的理解。我非常感激你的帮助。这是我现在有的。我只是用从表单中获得的用户名创建一个文件夹。我只是不知道如何从另一个现有文件夹复制文件。我检查了这里是否有类似的东西,但没有找到任何东西。

<?php
$folder = "/";
$name = $_POST['name'];
$thisdir = getcwd();
$folderPath = $thisdir . $folder . $name;
$source = "index.html";
$desitination =  $folderPath;
copy($source, $desitination);
if(!file_exists($folderPath)){
  mkdir($folderPath);
  chmod($folderPath,0777);
}
?>

在php中复制函数可以接受两个名称,而不仅仅是目录。

所以

copy("file1.txt", $folderPath)

copy("file1.txt", $folderPath . "/file1.txt")
并使用错误反馈来更好地理解
if (copy("file1.txt", "folder1/file1.txt")) {
    echo "File Copied";
} else {
    $errors= error_get_last();
    echo "File not copied, " . $errors["messages"];
}