Search code examples
phpmysqlrenamemysql-insert-id

PHP rename file to this sql id


I have the following lines of php code which should rename a file which already exists on the server to the id generated in the last query:

$image1Oldname = "images/" . $myfile;
$image1NewName = "images/" . mysql_insert_id() . ".jpg";

rename($image1Oldname, $image1NewName);

For some reason this does not rename the file when the script is run.

Any suggestions?


Solution

  • Any suggestions?

    Sure.
    You have to learn how to debug your code.

    It is not a rocket science though.

    • Just turn all possible error reporting on
    • and echo out every piece of data you are using to see if something goes wrong.
    ini_set('display_errors',1);
    error_reporting(E_ALL);
    
    $image1Oldname = "images/" . $myfile;
    $image1NewName = "images/" . mysql_insert_id() . ".jpg";
    
    var_dump($image1Oldname, $image1NewName);
    rename($image1Oldname, $image1NewName);
    exit;