I have a table in a database with the following fields current_filename
and new_filename
and I would like to rename the current filenames in a directory to their new filenames. I would also like to ensure that if the new filename already exists in the directory, then to overwrite the existing file with the newly renamed file.
A couple other points to make:
- current_filename is in the same directory as the soon-to-be newly renamed file
- there are some files in the directory that I do not want to rename
- I think I need to change the permissions on the folder and or files? I currently have the directory folder set to 777 and the (PDF) file permissions set to 644
Here is the PHP Script I have, but as a newbie to PHP, I'm not sure if it will work (or if I even needed to upload these new_filenames into the database to begin with).
<?php
require_once "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$current_filename = $dbh->query("SELECT current_filename FROM a_temp_rename");
$new_filename = $dbh->query("SELECT new_filename FROM a_temp_rename");
foreach($array as $current_filename=>$new_filename){
rename($current_filename,$new_filename);}
?>
NEW CODE
Based on an answer below, I have corrected the PHP and also included a variable for the directory. I have the following unanswered questions:
- Must I change the file and/or folder permissions from the current 644 to 777 and must I do this for all files and the folder or just the folder? (the folder is currently set to 777 but the PDF files are set to 644
- Did I have to upload these current_filenames and new_filenames into the db or was there a way to do this just using PHP and defining the key => value pair within PHP?
- Will the
$dir
variable I have set up for the path work when I concatenate it with the values from the db in the rename function? And must I make this directory an absolute or relative path? I have the PHP in the same directory as the files I would like to rename.
Here is the revised PHP Script:
<?php
require_once "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$filename = $dbh->query("SELECT current_filename,new_filename FROM a_temp_rename");
$dir = "./"
foreach($filename as $file){
rename($dir . $file['current_filename'],$dir . $file['new_filename']);}
?>
ANSWERS TO QUESTIONS
- I only have to change the folder permissions (not each individual file) to 777
- Unanswered.
- Setting a variable equal to the directory and concatenating the string in the rename function will work and I can use relative or absolute paths to rename the file.
ERROR!
I ran the test code and got the following error:
Parse error: syntax error, unexpected T_FOREACH in /public_html/_documents/test_rename/test_rename.php on line 12
Here is the code I ran, with all it's corrections noted above:
<?php
require_once "config.php";
$dbh = new PDO($dsn, $dbuser, $dbpass);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$filename = $dbh->query("SELECT current_filename, new_filename FROM a_temp_rename");
$dir = ("./_documents/test_rename/");
foreach ($filename as $file) {
rename($dir . $file['current_filename'], $dir . $file['new_filename']);
}
?>