Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: rename()

  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts

    rename()

    I'm trying to use rename to rename a list of files to the md5 of the same thing.

    Here is the code

    PHP Code:
    for($i 1;$i 124;$i++) {
        if (
    rename("image_".$i.".jpg",.md5("image_".$i).".jpg")) {
            echo 
    "$i done1<br>";
        }

    It keeps returning this error (124 times).

    Warning: rename(image_1.jpg,09c8440f6189657b3d096c0af86c07c8.jpg): No such file or directory in /home/user/public_html/images/a.php on line 24
    But the files do exist. Anyone know the problem? I don't want to have to rename 124 file

  • #2
    Regular Coder
    Join Date
    Apr 2004
    Posts
    102
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    for($i 1$i 124$i++)
    {
        if (
    file_exists("image_".$i.".jpg"))
        {
            if (
    rename("image_".$i.".jpg"md5("image_".$i).".jpg")) 
            {
                echo 
    "$i done1<br>";
            }
        }
        else 
        {
            echo 
    "File: (image_".$i.".jpg) doesnt exist"
        
    }

    You may also want to try the absolute path as well.

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    i personally dont like hard coding values into a loop as well. means you have to change it everytime you add something...

    if you can id try do a count on all items your looping through before you actually do the loop. store the number in a var then go

    for($i = 1; $i < $num; $i++)

    will work out easier in the long run

  • #4
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I solved it.

    The script was only gonna be run once, i changed the way my site was working and had to encode the way images were stored.


  •  

    Posting Permissions

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