【问题标题】:php - script to fix folder structurephp - 修复文件夹结构的脚本
【发布时间】:2011-08-04 14:18:33
【问题描述】:

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

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)

感谢大家的帮助。

【问题讨论】:

    标签: php io directory


    【解决方案1】:

    你试过php的重命名吗:

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

    【讨论】:

      【解决方案2】:
      // 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.';
      }
      

      【讨论】:

        【解决方案3】:

        难道你不能在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
        

        【讨论】:

        • @tkuzzy 如果我理解正确,x 和 y 是数字。
        • 当 x 和 y 是随机整数时,您不能只 cd "uploads/item/x/subitem/y/"
        • 抱歉,编辑了我的答案。您仍然不必使用 PHP 来执行此操作。
        • 无权访问 bash 终端,目前正在使用 windows。
        • 感谢您的帮助 tkuzzy,但我严格要求提供 php 解决方案。我知道 bash 和 windows 命令行很强大,可以单独完成,但 php 是我最擅长的语言。我更新了我在原始问题中寻找的内容,它类似于你的批处理答案,只是在 php.ini 中。干杯。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多