2011-08-04 20 views
2

,我改變我的方式保存在我的網站的後端文件:PHP - 腳本來修復文件夾結構

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) 

感謝您的幫助球員。

回答

0

你不能只是創建一個文件夾report/y/下,然後將所有文件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 
+0

沒有循環是必要的。 – tskuzzy

+0

@tskuzzy如果我理解正確,x和y是數字。 – zaf

+0

當x和y是隨機整數時,你不能只用cd「uploads/item/x/subitem/y /」 – user877756

0

您是否嘗試過PHP的重命名:

rename("uploads/item/x/subitem/y","uploads/item/x/subitem/y/report"); 
0
// 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.'; 
} 
相關問題