Why isn't this working? php rename()

Discussion in 'PHP' started by tobydawson13, Jan 28, 2011.

  1. #1
    Hi there,
    I have this script below, which should rename a file with a word randomly picked from a txt file. Please could somebody tell me why it is not working?
    Thanks

    <?php
    $filename="words.txt";
    $words=file($filename);
    shuffle($words);
    $word = $words[0];
    
    $old = 'tmp/today.php';
    $new = 'tmp/'.$word.'.php';
    rename($old, $new) or die("Unable to rename $old to $new.");
    ?>
    PHP:
    txt file
    adult
    aeroplane
    air
    airforce
    airport
    album
    alphabet
    apple
    arm
    PHP:
    Thanks
     
    tobydawson13, Jan 28, 2011 IP
  2. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #2
    You probably don't have rights to that files/folders.
    Try to chmod it to 777
     
    G3n3s!s, Jan 28, 2011 IP
  3. tobydawson13

    tobydawson13 Active Member

    Messages:
    645
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #3
    I've tried that.
    I've also noticed that the script works fine just like this:

    <?php
    $old = 'tmp/today.php';
    $new = 'tmp/tomorrow'.php';
    rename($old, $new) or die("Unable to rename $old to $new.");
    ?>
    PHP:
    and the part which gets the words also works fine on its own:

    <?php
    $filename="words.txt";
    $words=file($filename);
    shuffle($words);
    $word = $words[0];
    
    echo $word;
    ?>
    PHP:
    It's just when you join them both together that it doesn't work :S
     
    tobydawson13, Jan 28, 2011 IP
  4. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try using trim()
    
    $word = trim($words[0]);
    
    PHP:
     
    hogan_h, Jan 28, 2011 IP
    tobydawson13 likes this.
  5. tobydawson13

    tobydawson13 Active Member

    Messages:
    645
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    60
    #5
    Finally!! Thanks for that :)
     
    tobydawson13, Jan 28, 2011 IP
  6. val67

    val67 Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Try this :
    
    <?php
    copy('x.txt', 'copy.txt');
    unlink('x.txt');
    ?>
    
    Code (php):
     
    val67, Jan 29, 2011 IP
  7. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #7
    he solved it already val
     
    G3n3s!s, Jan 29, 2011 IP