SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2002
    Location
    Reykjavik, Iceland
    Posts
    104
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a rename script for my image gallery

    Hello can someone point to even a dos batch script or a php script that renames a hole lot of files to an filename that i have set (maybe a rule)...

    example:

    everyone getīs the files from their digital cams
    DCP_0001.jpg DCP_0002 and so on....

    I a one folder i would like to have all the images on the server for the gallery.. although they are maybe in a diffrent gallery...

    so if there are 127 images the highest filename is likeable to be 127.jpg..

    so i can rename all the DCP_000? to a numbered file name STARTING FROM 127 so now, i have new pics that are from 128.jpg to 211.jpg ready to be dumped to the folder that hold that pics all together..


    This could be an simple command line batch script... , a php script, or maybe an function on some image tool program that i can download from the internet and havenīt discovered yet

    Am not asking for that someone could code this for me, maybe you know of something similar php code that i could take a look at and hopefully change to the function that iīm looking for


    Thankīs in advanced..

  2. #2
    SitePoint Zealot jammo's Avatar
    Join Date
    Dec 2003
    Location
    el paso tx
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no reply yet huh?

    i too would like to know HOW to use the rename() function of PHP.

    << im also a newbie >>

  3. #3
    SitePoint Addict Messiah's Avatar
    Join Date
    Jun 2001
    Location
    Bloomington, In.
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, since there haven't been any takers on this one yet, I decided to sit down ( ok, so I was already sitting down ) and write it up. Be warned this has been tested to work, however, I can not guarantee anything. That said, use at your own risk.

    Here's the code:

    PHP Code:
    <?php    

    // function to rename group of files
    // takes the following variables as parameters
    // $path = full server path to directory of images
    // $start = integer starting point used in renaming
    // $type = the type of images allowed for renaming

    function rename_files$path$start$type )
    {
        
    // open directory
        
    $handle opendir $path ) or die( "Path is not a valid directory path!" );

        
    // loop through files in directory
        
    while ( $file readdir $handle ) )
        {
            
    // add only file types passed as $type to array of files to be renamed
            
    if ( substr $file, -) == $type )
            {
                
    // populate array
                
    $filelist[] = $file;
            }
        }
        
    // close directory
        
    closedir $handle );

        
    // only perform rename if array exists
        
    if ( $filelist )
        {
            
    // loop through array
            
    while ( list ( $key$value ) = each $filelist ) )
            {
                
    // set start point
                
    $i $key $start;

                
    // uncomment next line to activate renaming
                //rename ( $value, $i . $type );

                // debug line
                
    echo "file: <b>" $value "</b> | will be renamed to: <b>" $i $type "</b><br/><br/>";

            }
        }

        
    // otherwise no images were found
        
    else
        {
            echo 
    "No valid image types exist in directory: " $path;
        }
    }

    // you can call it like so
    rename_files $_SERVER['DOCUMENT_ROOT'] . "/images/flags/"127".gif" );

    ?>
    I wrote a function that takes a path to a directory of images, as well as a start value used for where to start when renaming the files found. Note that I included a debug line and commented out the actual rename() line so you can test it without worrying about accidentally renaming anything from the get go. Test it out and let me know if it does what you wanted. If it appears to function as you desire, simply uncomment the rename() line and remove or comment out the debug line.
    Messiah | Ink-Press: web publishing simplified!

  4. #4
    SitePoint Zealot arthaus's Avatar
    Join Date
    Jun 2002
    Location
    Pasadena, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Messiah,

    Thankx for your script.

    Q: How can I add a constant, like "img_" before the number?
    i.e., img_127.gif, img_128.gif, etc...

    Thankx in advance,
    Arthur

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2003
    Location
    Las Vegas
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    //rename ( $value, $prefix . $i . $type );

    where prefix is something you'll need to make sure fits into the schema provided by Messiah. You'll also probably want to edit his function to accept the prefix parameter.

    For the first poster here, you'd do best to read the manual:

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

    Since... well, that's what it's there for.


  6. #6
    SitePoint Addict Messiah's Avatar
    Join Date
    Jun 2001
    Location
    Bloomington, In.
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rabbit pretty much answered this one, however, heres the updated full code to add support for prefixing the name with a string of your choice:

    PHP Code:
    <?php     

    // function to rename group of files 
    // takes the following variables as parameters 
    // $path = full server path to directory of images 
    // $start = integer starting point used in renaming 
    // $type = the type of images allowed for renaming 
    // $prefix = string to prepend to filename

    function rename_files$path$prefix$start$type 

        
    // open directory 
        
    $handle opendir $path ) or die( "Path is not a valid directory path!" ); 

        
    // loop through files in directory 
        
    while ( $file readdir $handle ) ) 
        { 
            
    // add only file types passed as $type to array of files to be renamed 
            
    if ( substr $file, -) == $type 
            { 
                
    // populate array 
                
    $filelist[] = $file
            } 
        } 
        
    // close directory 
        
    closedir $handle ); 

        
    // only perform rename if array exists 
        
    if ( $filelist 
        { 
            
    // loop through array 
            
    while ( list ( $key$value ) = each $filelist ) ) 
            { 
                
    // set start point 
                
    $i $key $start

                
    // uncomment next line to activate renaming 
                //rename ( $value, $prefix . $i . $type ); 

                // debug line 
                
    echo "file: <b>" $value "</b> | will be renamed to: <b>" $prefix $i $type "</b><br/><br/>"

            } 
        } 

        
    // otherwise no images were found 
        
    else 
        { 
            echo 
    "No valid image types exist in directory: " $path
        } 


    // you can call it like so 
    rename_files $_SERVER['DOCUMENT_ROOT'] . "/images/flags/""img_"127".gif" ); 

    ?>
    Enjoy.
    Messiah | Ink-Press: web publishing simplified!

  7. #7
    SitePoint Zealot arthaus's Avatar
    Join Date
    Jun 2002
    Location
    Pasadena, CA
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thankx

    Hi Messiah,

    Thankx...I appreciate your support.
    It works perfectly!!

    Regards,
    Arthur

  8. #8
    SitePoint Addict Messiah's Avatar
    Join Date
    Jun 2001
    Location
    Bloomington, In.
    Posts
    216
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome. I am just glad someone got some use out of it. The original two who asked for it disappeared.
    Messiah | Ink-Press: web publishing simplified!


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
  •