rename function not working in loop

Discussion in 'PHP' started by sensoryaddict, Jun 2, 2011.

  1. #1
    Hi I cannot figure out why the rename function is not working as I think it should.

    Every aspect of the script seems to be working except for the rename function while inside the loop.

    <?php



    // open the current directory
    $dhandle = opendir('.');


    $rename = rename('test', 'wtfthisworks'); // changing directory test to wtfthisworks works!!


    if ($dhandle) {
    // loop through all of the files
    while (false !== ($fname = readdir($dhandle))) {
    // if the file is not this file, and does not start with a '.' or '..', and is a directory

    if (($fname != '.') && ($fname != '..') && ($fname != basename($_SERVER['PHP_SELF'])) && (is_dir($fname))) {


    $fname = trim($fname);

    $query = "'";


    $result = mysql_query($query) or die(mysql_error());

    $row = mysql_fetch_row($result); // query is working



    if ($row[0]) {



    $rename = rename('{$fname}', '{$row[0]}'); // this not working :(


    if($rename)
    {
    echo "success renaming {$fname} to {$row[0]} ! <br />";
    }
    else
    {
    echo "Renaming {$fname} failed to {$row[0]} <br />"; // the loop and variable print as expected therefore query and loop works!
    }


    }


    }
    }
    // close the directory
    closedir($dhandle);
    }

    :confused:
    mysql_close($pfw_link);

    ?>
     
    sensoryaddict, Jun 2, 2011 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    Hi, try this:
    $rename = rename("{$fname}", "{$row[0]}");
    PHP:
    Is it ok now? :)
     
    koko5, Jun 2, 2011 IP
  3. sensoryaddict

    sensoryaddict Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    yes!!

    Can you explain to me the difference. Thanks

    I am still learning the basic clearly.
     
    sensoryaddict, Jun 2, 2011 IP
  4. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #4
    koko5, Jun 2, 2011 IP