I have a directory of image files and I need a php script or shell script that will rename them, create a structure of nested directories, and then insert each image into a specified place in the directory hierarchy. Ideally I would just specify a parent directory for each file and a parent directory for each directory and it would build it. And then finally, I need the script to zip up the whole thing.
There's probably not an existing php class that will do all this for me, b开发者_StackOverflow社区ut if anyone knows of a php class or other script available online that would handle a lot of this logic that would be great.
I know its not PHP, but you might want to investigate something like this:
a shell script for renaming files (your dialect may vary, but the man pages are very helpful).
foreach img (/path/to/directory/*.jpg)
set newimg= `echo $img | sed 's,path/to/directory/(.+)\.jpg,new/path/$1newname.jpg/,'`
cp img newimg
end
If all the files in a particular directory are going to one location, something like the above might work. Essentially it loops through the target directory getting the names of the files that have a .jpg (or whatever) extension. Then it takes those names, including their path and subs in the new directory path and some change to the original file change. I've used ,
for the separator in the substitution because escaping all those path separators is a pain. Lastly, it copies the old file to the new location.
Since your directory needs are probably more complex than a hardcoded path allows for, you can include a line to parse your filepath/filename and determine what its target path should be; and use that in the substitution.
A snippet for creating a directory tree in one go can be found here.
You may also decide that find
is a better fit for this than foreach
because it can descend a directory structure as far as you like.
精彩评论