Jump to content


Photo

Renaming Image Files Script


  • Please log in to reply
7 replies to this topic

#1 1internet

1internet

    Advanced Member

  • Members
  • PipPipPip
  • 137 posts

Posted 06 December 2012 - 07:39 PM

I have about 500 images that are named like img_04352.jpg for example. I want to rename them all the the name of the page e.g. my-restaurant.jpg. Is there some script I can run to go through all the images and rename them?

#2 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,384 posts
  • LocationCanada

Posted 06 December 2012 - 07:46 PM

I must be misunderstanding what you really want. You can't name them all the same name. Are you wanting to name them something like: my-restaurant_0.jpg, my-restaurant_1.jpg, my-restaurant_2.jpg, etc.? Or, are these images for different pages and each one will have a unique name?

If you are wanting them all to be the same name with a different number associated with them, then just use Windows to rename them. I think you need Vista or higher for this. Just select all the images, then right-click and select rename. Windows will rename all of them and append a number after each one.

However, if you want all the images to have different names I can't provide a solution because you haven't provided any information on how you are going to relate the image to the page. I.e. what logic would be used to know img_04352.jpg should be renamed to my-restaurant.jpg
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#3 1internet

1internet

    Advanced Member

  • Members
  • PipPipPip
  • 137 posts

Posted 07 December 2012 - 05:13 AM

Right ok sorry for the confusion. Say I have a directory of restaurants, and they have images like img_003452.jpg, and I want to rename it to the restaurant name, then how would I do that?

#4 White_Lily

White_Lily

    Advanced Member

  • Members
  • PipPipPip
  • 521 posts

Posted 07 December 2012 - 08:32 AM

The script below is my upload script that renames files upon upload, however im sure it can be manipulated to rename files in a directory!

<?php
if(!empty($_POST["upload_file"])){
$file = explode(".", $_FILES["file_upload"]["name"]);
$extension = strtolower($file[1]);

if(!empty($file[1])){
if(in_array($extension, array('jpg','jpeg', 'gif', 'png', 'bmp', 'doc', 'pdf', 'docx', 'mp4', 'zip', 'mov', 'mpg', 'wmv', '3gp'))){
$filepath = $_FILES["file_upload"]["tmp_name"];
$filename = uniqid().".".$file[1];
$target = $_SERVER["DOCUMENT_ROOT"]."template/uploads/".$filename;
if(move_uploaded_file($filepath, $target)){

$document = str_replace("_", " ", $file[0]);
$document = str_replace("-", " ", $document);

$url = $GLOBALS["siteUrl"]."uploads/".$filename;

$putFileInfo = insert("files", "file_name, file_url, file_original, new_file", "'$document', '$url', '".$_FILES["file_upload"]["name"]."', '$filename'");

if($putFileInfo){
$filePass = "File was successfully uploaded.";
}else{
$fileErr = "File could not be uploaded: ".mysql_error();
}

}else{
$fileErr .= "Could not move file.";
}
}else{
$fileErr .= "That is not an accepted file extension for the $page.";
}
}elseif(empty($file[1])){
$fileErr = "No file selected.";
}
}
?>

"In order to be irreplaceable, one needs to be different from another."

CMS Development: http://cms.janedeaslart.co.uk/


#5 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,384 posts
  • LocationCanada

Posted 10 December 2012 - 01:41 AM

Right ok sorry for the confusion. Say I have a directory of restaurants, and they have images like img_003452.jpg, and I want to rename it to the restaurant name, then how would I do that?


I'm still at a loss as to how you would determine what an image should be renamed to. You give the example of renaming img_003452.jpg to my-restaurant.jpg. How would that be determined? In other words, why wouldn't that image be renamed to some-other-restaurant.jpg?
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net

#6 1internet

1internet

    Advanced Member

  • Members
  • PipPipPip
  • 137 posts

Posted 10 December 2012 - 06:53 PM

Ok, so I can determine it through the database. So if we run something like
$result = mysql_query(SELECT * FROM `restaurants`);
while ($row = mysql_fetch_assoc($result)) {
rename('images/$row['image']', $row['name'])
}
So there will be some sort of function (rename), that locates the image, and then the second argument gives its name to rename. Sure some valdation is necessary for $row['name'], but thats easy so dont worry about that, lets assume the name is e.g. my-restaurant.jpg

#7 Christian F.

Christian F.

    Advanced Member

  • Staff Alumni
  • 3,106 posts
  • LocationNorway

Posted 11 December 2012 - 03:06 AM

http://php.net/rename
Keeping it simple.

#8 Psycho

Psycho

    Advanced Member

  • Gurus
  • 10,384 posts
  • LocationCanada

Posted 11 December 2012 - 10:26 AM

Wow, you could have saved yourself and us a ton of time if you had stated that the images were associated with the restaurant names in a database.

I assume that you will need to update the database record with the new name since 1) it is possible, however unlikely, that two restaurants could have the same name and/or you might want to support multiple pictures at some point. And 2) You will need to replace some characters in the name to prevent problems with the file.

I would also expect that you will be updating the upload script to name the files based upon the restaurant name too. So this script should be used as a one time operation to update the existing images. So, you want to do two things. 1) Create a function to generate the file name based upon the restaurant name. You will use this in your current upload script and for the batch update script for current images. 2) Create the batch update script to get the data from the database and update current images and the update the DB records with the new name.
The quality of the responses received is directly proportional to the quality of the question asked.

I do not always test the code I provide, so there may be some syntax errors. In 99% of all cases I found the solution to your problem here: http://www.php.net




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com