php - script to fix folder structure
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.
Have you tried php's rename:
rename("uploads/item/x/subitem/y","uploads/item/x/subitem/y/report");
// 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.';
}
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
精彩评论