SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Ohio, USA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Moving/renaming files

    I made a similar thread last night, though I didn't sit down to think it out... stupid me. So I'll explain it again, in a more realistic tone.

    I read through this, which is similar to what I need http://www.sitepoint.com/forums/show...t=rename+files Theorectically: I have a directory with 1,000+ images. I need a script that'll move the images into another directory and then rename the images, themselves. Let's say I need to make 3 directories, representing 3 months, in my main image directory. Each of these 3 would have 31 other directories, representing days. Finally, these 31 directories would contain 24 images. The images in each directory would need to be renamed 1.jpg to 24.jpg. When the files are all moved/renamed, it should be similar to:

    (Dir structure)
    images/1/1/1.jpg (image folder/month/day/image number)
    1/1/1/2.jpg
    ...
    ...
    ...
    1/1/1/24.jpg

    I'm still relatively new to PHP but I have yet to figure out how to do this, even with php.net. Does anyone have a clue as to how this would be done? Thanks in advance.

  2. #2
    SitePoint Zealot Scott.Mc's Avatar
    Join Date
    Jul 2004
    Location
    Scotland
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well if its a linux box, just execute the mv blah.jpg /new/dir/blah.jpg

    and use mkdir newdir

    to create a new dir

    php uses exec("command");
    or you can get the output with passthru("command", $output");
    Linux Server Management - AdminGeekZ.com
    Is your website Sluggish? Unavailable? Insecure?

    Why not call us? +44 0141 2800134

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Ohio, USA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BreakOutHosting
    well if its a linux box, just execute the mv blah.jpg /new/dir/blah.jpg

    and use mkdir newdir

    to create a new dir

    php uses exec("command");
    or you can get the output with passthru("command", $output");
    Exactly, how would I do this? lol I'm still a pretty new PHP coder & I have no experience with any of this...

  4. #4
    SitePoint Guru
    Join Date
    Jul 2004
    Location
    Raleigh, NC
    Posts
    783
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you may just want to read up on php's rename() function. it's great. you actually rename a file's full path. so if you rename /img/random/blah/here/1.jpg to img/newname.jpg then you've moved it and renamed it. so read through each file a directory using dir() and if it's an image, rename it. here are some links to get you started:

    http://www.php.net/manual/en/function.rename.php
    http://www.php.net/manual/en/ref.dir.php

    oh yeah, and if you're feeling advanced, you may want to first see if the new file name already exists so you don't accidently overwrite a file

  5. #5
    SitePoint Zealot Scott.Mc's Avatar
    Join Date
    Jul 2004
    Location
    Scotland
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    file_exists()

    basicly rename does the same as what i said mv is a linux command, mv just executes that with the 2 strings given.
    Linux Server Management - AdminGeekZ.com
    Is your website Sluggish? Unavailable? Insecure?

    Why not call us? +44 0141 2800134

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2004
    Location
    Ohio, USA
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Darchangel
    you may just want to read up on php's rename() function. it's great. you actually rename a file's full path. so if you rename /img/random/blah/here/1.jpg to img/newname.jpg then you've moved it and renamed it. so read through each file a directory using dir() and if it's an image, rename it. here are some links to get you started:

    http://www.php.net/manual/en/function.rename.php
    http://www.php.net/manual/en/ref.dir.php

    oh yeah, and if you're feeling advanced, you may want to first see if the new file name already exists so you don't accidently overwrite a file
    Thanks. I read through these link but I'm still lost. I'm not terribly good at coding (I just recently learned to control variables with the server's date). Could anyone offer a code sniplet so I'd know where to begin? I'm not asking anyone to code it all for me. I just need a starting place. I'm learning from experience so this could help me greatly. As always, thanks in advance.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •