PHP:移动包含内容的目录?

发布于 2024-09-17 20:00:51 字数 705 浏览 1 评论 0原文

我有一个存储文件名和目录名的数组 $bundle

我正在使用 foreach 循环遍历数组,我想将它们移动到另一个目录中。因此,我使用重命名方法,它对于 JUST FILES 效果很好。

但是,其中包含其他文件的目录不会响应 rename() 方法。

$folder =  'files';
foreach ($bundle as $value) {
    $ext = pathinfo($value, PATHINFO_EXTENSION);
    if ($ext != "") { //if $value has no suffix it's a fil
        rename(PATH . '/' .$value, $folder . '/' . $value);
    }

    if ($ext == "") { // it's a folder/directory
        //rename doesn't work for directories with contents
        //what method should i use here???
    }

}

我知道 pathinfo() 方法不是确定它是否是目录的最佳方法,但是对于我的小项目来说它很好。我只需要知道如何将任何目录及其所有内容移动到“文件”文件夹中。

感谢您的帮助。

I'm having an array $bundle that stores filenames and directory names.

I'm running through the array with a foreach loop and I want to move them inside of another directory. I'm using the rename method therefore and it works pretty fine with JUST FILES.

However directories with other files in there don't respond to the rename() method.

$folder =  'files';
foreach ($bundle as $value) {
    $ext = pathinfo($value, PATHINFO_EXTENSION);
    if ($ext != "") { //if $value has no suffix it's a fil
        rename(PATH . '/' .$value, $folder . '/' . $value);
    }

    if ($ext == "") { // it's a folder/directory
        //rename doesn't work for directories with contents
        //what method should i use here???
    }

}

I know the pathinfo() method is not the best way to find out if it's a directory or not, however for my little project it's fine. I just need to know how I can move any directory with all it's contents into the "files" folder.

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趴在窗边数星星i 2024-09-24 20:00:51

您必须使用 globscandir 获取该目录中的所有文件名。然后你必须通过重命名循环它们并移动它们。

如果您的主机允许,您的另一个选择是使用 shell_exec 并为 Linux 执行 mvcopy / xcopy for windows 命令并以这种方式移动它们。如果您选择 exec 路由,请确保保护输入等,以防止发生任何不好的事情。

You will have to get all the filenames in that directory using glob or scandir. Then you would have to loop through them with the rename and move them.

Your other option, if you host allows it, is to use shell_exec and do the mv for linux or copy / xcopy for windows command and move them that way. If you choose the exec route, make sure to secure the input etc to prevent any bad stuff from happening.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文