0

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:

  1. 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
  2. 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?
  3. 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

  1. I only have to change the folder permissions (not each individual file) to 777
  2. Unanswered.
  3. 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']);
    }

?>
Ben
  • 1,013
  • 4
  • 16
  • 34

2 Answers2

1
<?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");


foreach($filename as $file){
rename($file['current_filename'],$file['new_filename']);}

?>

Here for rename source and destination should be proper paths (NOT JUST FILE NAME)

ref : http://php.net/manual/en/function.rename.php

Prasanth Bendra
  • 31,145
  • 9
  • 53
  • 73
  • Thank you @prasanthbendra . To complete your answer, could you please comment on whether I needed to upload these new_filenames into the database to begin with or was there another way I could have gone about doing that? I currently have the list of names in an excel spreadsheet. I read that the rename function will overwrite any existing files with the new_filename which is what I am looking for, so that is good. Also, do I need to change the permissions on the folder and files or just the folder and what permission must I set it to? Lastly, could I perhaps use a variable for the directory? – Ben Jan 28 '13 at 07:05
  • @BenJones : As you already have this code, you import your excel sheet to mysql database(Convert it to csv, then in phpmyadmin there is a option to import). You can add directory name as a variable in that case your rename will be rename($dir_name."/".$file['current_filename'],$dir_name."/".$file['new_filename']); You give write permission for the folder. – Prasanth Bendra Jan 28 '13 at 07:17
  • I have done as you describe - saving the excel file as a csv and then importing the file into MySQL using PHP MyAdmin. This seems to me the best way to do this kind of a change. I suppose I could have included the filenames within the PHP script as well, like as a key=>value pair as described above in my revision to the question. My only remaining question is: must I use a direct path to the files (i.e. "www.website.com/...") or can I use a path relative to where the PHP file is located, the curent directory (i.e. "./"). Answer has been accepted. Thanks so much for your help. – Ben Jan 28 '13 at 07:29
  • I have run the code you provided on a test files in a test directory but it produced a syntax error on Line 12 due to an unexpected `T_FOREACH`. I have researched this error and I have inspected the code and I don't think I am missing ant `;`. Please let me know why the code is producing this error. Thanks in advance. – Ben Jan 28 '13 at 08:00
  • print_r($filename); above the foreach and see what is coming there – Prasanth Bendra Jan 28 '13 at 08:42
  • You are missing ; in line $dir = "./" – Prasanth Bendra Jan 28 '13 at 08:43
0

You can use the rename function in PHP to rename the file in a directory in the server. Please refer the following links for examples.

http://php.net/manual/en/function.rename.php

Bulk Rename Files in a Folder - PHP

Community
  • 1
  • 1
Edwin Alex
  • 5,118
  • 4
  • 28
  • 50
  • Thanks, I am aware of the `rename` function in PHP but the manual only has an example with one file being renamed. The example you provided, which I have already read, shows how to create a loop to access all files and rename them, but the example includes a constant new_filename ("SKU") whereas I would like to use an associative array to rename the files and `foreach` current_name `rename` the files to their new_filename. This question is substantially different than the examples you have provided. – Ben Jan 28 '13 at 07:00