Jump to content

Renaming Image Files Script


1internet

Recommended Posts

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

Link to comment
Share on other sites

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.";
}
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.