|
|
[PHP] mkdir and rename on localhost
|
|
|
|
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status:
Offline
|
|
I have a directory with a whole bunch of images (>1,000) with the following types of names:
Mary 1.jpg
Mary 2.jpg
Mary 3.jpg
John 1.jpg
John 2.jpg
Nick 1.jpg
Nick 2.jpg
Nick 3.jpg
Nick 4.jpg
Martha 1.jpg
Martha 2.jpg
– etc. I need to sort these into folders, so that for “Mary 1/2/3.jpg”, for instance, a new folder named “Mary” is created, and the three files are put into this folder and renamed just “1/2/3.jpg” (and so on for the other pictures as well).
The easiest way I could think of (not necessarily actually the easiest way) to do that was in PHP: strip (and save) the extension [$ext]; strip (and save) the number [$fnr]; strip (and save) the name, to be used for the new directory [$new_dir]; then do more or less the following:
Code:
if (is_dir($new_dir)) {
rename($file, $new_dir . $fnr . $ext);
} else {
mkdir($new_dir);
rename($file, $new_dir . $fnr . $ext);
}
– stuff everything into a browser, and let it move and rename the files for me when I run the function/open the page.
That, however, does not work. It doesn’t give me any errors or anything, but it doesn’t actually do anything, either. I have verified that it does strip the abovementioned variables correctly, and that it does check for (and find, when they exist) the appropriate directories—but rename and mkdir seem to have no effect. Even if I put either of the two functions all by themselves and give them straightforward values (i.e., “mkdir('blah');”), they do nothing.
I’ve tried looking through various parts of php.net till my head started turning octagonally ochre, and I can’t figure it out. I’m sure there’s something quite simple I’m overlooking, but I can’t see it.
Help?
|
|
|
|
|
|
|
|
|
Senior User
Join Date: Jul 2004
Status:
Offline
|
|
add this at the top before the script
Code:
if(function_exists(mkdir))
echo 'You can Make Directories<br/>';
if(function_exists(rename))
echo 'You can rename Directories<br/>';
This will tell you if you can make directoires or rename them. Some hosts will disable this for security.
|
You shouldn't make fun of nerds... you'll be working for one some day.
|
|
|
|
|
|
|
|
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status:
Offline
|
|
Yup, they both seem to exist—would be odd otherwise, since it's my own PowerBook that's the host...
|
|
|
|
|
|
|
|
|
Senior User
Join Date: Jul 2004
Status:
Offline
|
|
sometimes you need to specific exactly were you want it. like
mkdir('MacintoshHD/Users/You/Sites/'.$newdir);
see if that works.
btw. what version of php you running?
|
You shouldn't make fun of nerds... you'll be working for one some day.
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status:
Offline
|
|
Does your webserver have permission to edit those files and create/rename directories? That's the most common issue i run into doing file stuff.
You shouldn't (emphasis on shouldn't have to have absolute filenames, but if all else fails, it's worth a shot. Make sure you have the permissions for the dirs and files correct.
|
2008 iMac 3.06 Ghz, 2GB Memory, GeForce 8800, 500GB HD, SuperDrive
8gb iPhone on Tmobile
|
|
|
|
|
|
|
|
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status:
Offline
|
|
Yes! I finally managed to get it right—it turns out I’d forgotten to 777 the folder where the images were supposed to go. What a dumb thing to miss for a day and a half. *blush*
Thanks, all, for helping me go through things orderly and finding the leak
|
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status:
Offline
|
|
Interesting choice of languages to solve that problem. I'd have done a shell script.
|
|
|
|
|
|
|
|
|
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status:
Offline
|
|
Yeah, I would too—if I knew the first thing about how to do that
The only reason I chose PHP is that it's more or less the only scripting language I know just minimally (apart from JavaScript, which would be even worse for this)...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Forum Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|