Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > [PHP] mkdir and rename on localhost

[PHP] mkdir and rename on localhost
Thread Tools
Oisín
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Jun 4, 2006, 01:17 PM
 
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?
     
jay3ld
Senior User
Join Date: Jul 2004
Status: Offline
Reply With Quote
Jun 4, 2006, 02:03 PM
 
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.
     
Oisín  (op)
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Jun 4, 2006, 02:32 PM
 
Yup, they both seem to exist—would be odd otherwise, since it's my own PowerBook that's the host...
     
jay3ld
Senior User
Join Date: Jul 2004
Status: Offline
Reply With Quote
Jun 4, 2006, 06:49 PM
 
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.
     
SirCastor
Professional Poster
Join Date: Jan 2001
Location: Salt Lake City, UT USA
Status: Offline
Reply With Quote
Jun 4, 2006, 11:40 PM
 
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
     
Oisín  (op)
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Jun 5, 2006, 12:08 PM
 
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
     
wataru
Addicted to MacNN
Join Date: Oct 2001
Location: Yokohama, Japan
Status: Offline
Reply With Quote
Jun 10, 2006, 08:28 PM
 
Interesting choice of languages to solve that problem. I'd have done a shell script.
     
Oisín  (op)
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Jun 11, 2006, 08:28 AM
 
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)...
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 02:58 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,