Sorry, I did not know how to give it a better name and that´s my first thread here.
The problem I have is... I have a bunch of images that have filenames all in this syntax:
ABC_1234-sometext.jpg
Just renaming it in php would not be a big deal but what I want is, I want to get the
1234
which is unique for each image (let´s call it products_ean) and do a SELECT query in my database which gives me back a different ID poducts_id) for each image which is also unique.
So what I want is to get
ABC_1234-sometext.jpg
into
56789.jpg
Where as the numbers are related by the database. Does that make sense? I hope somebody could push me in the right direction.
This would be the query that result in the wanted image filename:
SELECT products_id FROM products WHERE products_ean = '.$img_name_strg.'
You can use the following code and amend the filename to whatever you wish.
I use round(microtime(true))
for a unique name as opposed to a DB id.
$temp = explode(".", $file["name"]);
$filename = "filename"
$newfilename =$filename . '.' . end($temp);
move_uploaded_file($file["tmp_name"], $target_dir . $newfilename);
Don't forget to check if your file has been uploaded.
Try the below code.
$files = glob($dir.'/*'); //get all images
$i=1234;
foreach($files as $file){
if(is_file($file)){
copy($file,'folder/'.$i'.jpg');//copy ABC_1234-sometext.jpg to 1234.jpg
unlink($file); //delete ABC_1234-sometext.jpg
}
}
You can create a query to fetch images clumn and change image names with new random names without changing extention. Or load them on a page then rename all with new name and update table. to get first part of images use :
$file_name = $row['img'];
$image = explode('.',$file_name);
use This to change names $image
and then include their extentions
$chagedImage =$image . '.' . end($temp);
Use this value in your move_upload $chagedImage
UPDATE :
I think runing this query will do the job
function generatenewstring($lenght){
//This function creates random tokens for new name
$token = 'qwertzuiopasdfghjklyxcvbnmQWERTZUIOPASDFGHJKLYXCVBNM0123456789';
$token = str_shuffle($token);
$token = substr($token, 0, 36);
return $token;
}
$stmt = $pdo->prepare('SELECT * FROM your_table');
$stmt->execute();
$row = $stmt->fetchAll();
$id = $row['id'];
$image = explode('.',$row['img']);
$extension = pathinfo($image, PATHINFO_EXTENSION);
//$image this is your old image name
$chagedImage = generatenewstring(20) . '.' . $extension);
//$chagedImage this is your new image name
$sql = "UPDATE your_table SET image = ? WHERE id = ?";
$pdo->prepare($sql)->execute([$chagedImage, $id]);
I came up with this now. It works. But I am not sure if this will work if I have to rename a thousand images? I guess to have the query in the foreach is not a nice way to go?
$dir = '/original_file_directory';
$destination = '/destination_directory/';
$files = glob($dir.'/*'); //get all images
foreach($files as $file){
if(is_file($file)){
$ean = substr(basename($file), 8, 5);
$image_query = xtc_db_query("SELECT products_id FROM products WHERE
products_manufacturers_model = ".$ean."");
$image_id = xtc_db_fetch_array($image_query);
$image_id = $image_id['products_id'];
if($image_id != '')
copy($file,$destination.$image_id.'.jpg');
}
}
And what if I have files that would give me identical $ean and I would have to add ..._0.jpg and ..._1.jpg for those doubles?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.