php - 修复文件夹结构的脚本

发布于 2024-11-27 19:35:30 字数 443 浏览 1 评论 0原文

我正在更改在网站后端保存文件的方式,从:

uploads/item/x/subitem/y/document.pdf

其中 x 和 y 是整数 id,将其更改为:

uploads/item/x/subitem/y/report/document.pdf

我还需要更改当前文件夹目录,该目录有超过 1000 条记录。有没有一种好的方法可以在 php 中修复此结构,而无需处理嵌套循环中的每个文件夹,我只需要在文档之前添加另一个文件夹即可。

编辑:

这是我试图找到的命令!

foreach(glob('uploads/item/*/subitem/*/*') as $filePath)

谢谢你们的帮助。

I am changing the way I save files on the back-end of my website from:

uploads/item/x/subitem/y/document.pdf

Where x and y are integer id's, changing it to:

uploads/item/x/subitem/y/report/document.pdf

I need to change the current folder directory as well, which is over 1000 records. Is there a nice way to fix this structure in php without addressing each folder in nested loops, I just need to add another folder before the document.

EDIT:

This was the command I was trying to find!

foreach(glob('uploads/item/*/subitem/*/*') as $filePath)

Thanks for the help guys.

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

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

发布评论

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

评论(3

猫瑾少女 2024-12-04 19:35:30

您是否尝试过 php 的重命名:

rename("uploads/item/x/subitem/y","uploads/item/x/subitem/y/report");

Have you tried php's rename:

rename("uploads/item/x/subitem/y","uploads/item/x/subitem/y/report");
ㄟ。诗瑗 2024-12-04 19:35:30
// Change the current directory to the folder containing all your data
chdir('uploads/item/x/subitem/y');

// Attempt to create the report directory if it does not already exist
if ((file_exists('report') && is_dir('report')) || mkdir('report'))
{
    // Move every PDF file in the current directory
    foreach (glob('*.pdf') as $File)
    {
        rename($File, 'report/' . $File);
    }
}
else
{
    echo 'ERROR! Directory could not be created.';
}
// Change the current directory to the folder containing all your data
chdir('uploads/item/x/subitem/y');

// Attempt to create the report directory if it does not already exist
if ((file_exists('report') && is_dir('report')) || mkdir('report'))
{
    // Move every PDF file in the current directory
    foreach (glob('*.pdf') as $File)
    {
        rename($File, 'report/' . $File);
    }
}
else
{
    echo 'ERROR! Directory could not be created.';
}
赠意 2024-12-04 19:35:30

您不能在 y/ 下创建一个文件夹 report/,然后将 y/ 中的所有文件移至 report/?这根本不需要 PHP。

编辑:我的 bash 非常生疏,所以这里可能有很多语法错误。但这是基本思想:

for x in /fullpath/uploads/item/*
do
    for y in uploads/item/$x/subitem/*
    do
        cd /fullpath/uploads/item/$x/subitem/$y
        mkdir report/
        mv * report/
    done
done

Couldn't you just create a folder report/ under y/ and then move all files in y/ into report/? This doesn't require PHP at all.

EDIT: My bash is very rusty so there are probably a lot of syntax errors in here. But this is the basic idea:

for x in /fullpath/uploads/item/*
do
    for y in uploads/item/$x/subitem/*
    do
        cd /fullpath/uploads/item/$x/subitem/$y
        mkdir report/
        mv * report/
    done
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文