Hi

Apologies if this is not the correct forum for this question.

I am running xampp.

I have 100+ image files in a directory that I wish to rename.
The old filename and the new filename are stored in an mysql database
I want to write a routine that will rename the files based on this information.
It is only the filename not the extension that will change although currently the filename (old and new)in the database includes the extension

My 'variables' (I use the term very loosely as I am a php newbie)

database name
database user
database password

tablename - containing both old and new filenames columns
oldfilename - containing old filenames
newfilename - containing new filenames (Note - there are some instances of oldfilename which have no value and therefore should be skipped)

location of files to rename

Can anyone help me put this together please?

Thanks
Mark

Recommended Answers

All 10 Replies

Member Avatar for diafol

You've done most of the hard work yourself.
Get the data from the DB and put into a loop:

$r = mysql_query("SELECT oldfilename, newfilename, location FROM tablename WHERE oldfilename != ''");
while($d = mysql_fetch_array($r)){
//do you need a '/' between location and the filename?
  $old = $d['location'] . $d['oldfilename'];
  $new = $d['location'] . $d['newfilename'];
  if(file_exists($old)){
     rename($old, $new);
  }

}

perhaps you need to check the status of the file or directory if you get errors.
There may be an easier batch method, but I'm not aware of it.

Thank you very much for your reply.

I have created the conection to the database and all appears well apart from the location which is not in the database but rather I was going to hardcode.

The location is:
c:\xampp\htdocs\combined\images\avatars\upload\

If I add it as folows I think there is a syntax issue judging from the text colour change within edit plus?

$old = "c:\xampp\htdocs\combined\images\avatars\upload\" . $d['oldfilename'];
  $new = "c:\xampp\htdocs\combined\images\avatars\upload\" . $d['newfilename'];

Am I missing smething fundamental here?

Thanks
Mark

Yes, a backslash is an escape character. So if you want to use it, you'll have to escape it, so use \\ Note, that you are allowed to use a forward slash, even on Windows.

Thank you :)

I have swapped to foward slashes.

Regards
Mark

Just one last question - there may be the odd instance when a file should be renamed but doesn't exist.

Is there a way to echo out the filename that cannot be found?

Thanks
Mark

Change ardav's code to this:

if (file_exists($old))
{
  rename($old, $new);
}
else
{
  echo $old . ' does not exists.';
}

Thank you very much :)

And thank you to everyone on this board for your help.

Mark

Dare I ask one final question please?

I want to insert back into the database a different filename for each user_id where there has been a file renamed.

The code I now have (which works :) subject to line 21) is as follows:

$l = "c:/xampp/htdocs/combined/images/avatars/upload/";
$s = "6b24eebb52613c8392cc5449d74c06de";
$c = 0;
$n = 0;


$r = mysql_query("SELECT user_id, user_avatar FROM users WHERE user_avatar <> ''");

while($d = mysql_fetch_array($r)){

  $e = $d['user_avatar'];
  $ext = substr($e,-3); 
  $old = $l . $d['user_avatar'];
  $new = $l . $s . '_' . $d['user_id'] . '.' . $ext;
  $rep = $d['user_id'] . $s . '.' . $ext;

if (file_exists($old))
{
  rename($old, $new);
  $c = $c + 1;
  $result = mysql_query("UPDATE users SET user_avatar=".$rep" WHERE user_id=".$d['user_id']"");

}
else
{
  echo "<br />" . $old . " does not exist for member ID " . $d['user_id'];
  $n = $n + 1;
}



  }

echo "<br /><br />" . $c . " files (s) renamed";
echo "<br />" . $n . " files (s) not found";
echo "<br />Ended";
?>

This is producing the following error message:

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in C:\xampp\htdocs\combined\renameavatars.php on line 21

What I am trying to achieve with line 21 is:
Update the users table where user_id equal to $d and set user_avatar equal to the variable $rep

I am guessing it must be my syntax for inserting a variable which is causing the problem.


Many thanks
Mark

Member Avatar for diafol
$result = mysql_query("UPDATE users SET user_avatar='{$rep}' WHERE user_id={$d['user_id']}");

Thank you again for your help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.