renaming images in a folder - images deleted - php

I need to rename images in a folder i.e. give them unique names. Using this code images are not renamed but deleted !
$path = "../b-300x250/";
$items = glob($path . '*.jpg');
foreach($items as $img) {
$uniq = uniqid() . '.jpg';
rename("$img", "$uniq");
}

Your code is moving your images to a different directory.
Add the path to your unique name.
$path = "../b-300x250/";
$items = glob($path . '*.jpg');
foreach($items as $img) {
$uniq = $path . uniqid() . '.jpg';
rename("$img", "$uniq");
}

Here you must provide complete path or a valid path. In your code you are trying to rename files in current working directory but actually you have to work on ../b-300x250/ directory, So you should append this to make your code to rename a file correctly.
Change this:
rename("$img", "$uniq");
This:
rename("$img", $path.$uniq);
PHP code:
$path = "../b-300x250/";
$items = glob($path . '*.jpg');
foreach ($items as $img)
{
$uniq = uniqid() . '.jpg';
rename("$img", $path.$uniq);
}

Related

I need to create .Zip file in Laravel and my code does not work

this is the 1st time I posted on StackOverflow. I need to create a zip file of multiple images. I've tried Zipper and also ZipArchive and my code still fails.
$zip = new \ZipArchive();
foreach ($students as $student) {
$download = 'album' . $student->album_id . '.zip';
if ($zip->open(public_path('albums/' . $student->album_id . '/' . $download), \ZipArchive::CREATE) === TRUE) {
$files = Storage::allFiles('public/albums/' . $student->album_id . '/image_files');
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
}
}
I can assure that all the images exist. I put the images in Storage/app/public/albums/$student->album_id/image_files/. Please help me.
First, you can add an additional step to verify that your file exists by using PHP built in function: file_exists()
Most times, if you your files do noT exists, no files will be added to the zip, and your code will run without throwing any error but still will not work.
What you're passing to addFile() is not a correct path. addFile() needs an absolute path to where your file is stored. You will need to add this line $file_path = storage_path("app/{$file}");
See code below:
$zip = new \ZipArchive();
foreach ($students as $student) {
$download = 'album' . $student->album_id . '.zip';
if ($zip->open(public_path('albums/' . $student->album_id . '/' . $download), \ZipArchive::CREATE) === TRUE) {
$files = Storage::allFiles('public/albums/' . $student->album_id . '/image_files');
foreach ($files as $file) {
$file_path = storage_path("app/{$file}");
if (file_exists($file_path)) {
$zip->addFile($filepath);
}
}
$zip->close();
}
}
Optionally, if you wish to download any of the zipped files, you can return the download response at the end of your code to download:
return response()->download(public_path('albums/' . $student->album_id . '/' . $download));

How to check Image name in folder and rename it?

I have a csv file with image name and Prefix for image and I want to add prefix in that image name and rename it and move it to another directory.
<?php
$fullpath = '/var/www/html/john/Source/01-00228.jpg';
$additional = '3D_Perception_';
while (file_exists($fullpath)) {
$info = pathinfo($fullpath);
$fullpath = $info['dirname'] . '/' . $additional
. $info['filename']
. '.' . $info['extension'];
echo $fullpath ;
}
?>
Here Image file is store in Source Directory I want to rename it with some prefix and move it other directory like Destination
Please help me out to find solution for this.
<?php
$source_directory = '/var/www/html/john/Source/';
$dest_directory = '/var/www/html/john/Source/'; // Assuming you'll change it
$filename = '01-00228.jpg';
$prefix = '3D_Perception_';
$file = $source_directory . $filename;
$dest_file = $dest_directory . $prefix . $filename;
if (file_exists($file)) {
if (copy($file, $dest_file)) {
unlink($file); // Deleting source file.
var_dump(pathinfo($dest_file)); // Dump dest file infos, replace it with wathever you want to do.
} else {
// if copy doesn't work, do something.
}
}
?>
Try this, and take care about my comments. ;)

Upload multiple files and save in database laravel 4.2

Here is my controller code:
$images = Input::file('images');
foreach($images as $image){
$filenames = date('Y-m-d-H-i-s') . '-' . $image->getClientOriginalName();
$imageFolder = 'clients';
$image->move($imageFolder , $filenames);
echo $filenames;
}
I was trying to upload multiple image files and could do that. Now I want to save all the file names as an array or as whatever is the best in the database.
I tried to use serialize() laravel 4.2 says serialization is not allowed for uploading.
Any idea ? Thanks.
Try this ::
$images = Input::file('images');
$filenames = array();
foreach($images as $image){
$filenames [] = date('Y-m-d-H-i-s') . '-' . $image->getClientOriginalName();
$imageFolder = 'clients';
$image->move($imageFolder , $filenames);
echo $filenames;
}

PHP move files with specific name

I have 100.000+ files with name RMA_(NUMBER)(DATE)(TIME).jpg like RMA_12345_2015_10_12_17_00_35.jpg
How I can move this file like RMA_35200_*.jpg?
You can use command:
$ mv RMA_35200_*.jpg new_path
or use php for that, example:
<?php
$fromPath = __DIR__ . '/from';
$toPath = __DIR__ . '/to';
$files = glob("{$fromPath}/RMA_35200_*.jpg");
foreach ($files as $file) {
$fileName = basename($file);
rename($file, "{$toPath}/{$fileName}");
}
Use glob()to find those files and rename() to move them
function moveFiles($source, $target) {
// add missing "/" after target
if(substr($target,-1) != '/') $target .= '/';
$files = glob($source);
foreach($files as $file) {
$info = pathinfo($file);
$destination = $target . $info['filename'];
rename($file, $destination);
}
}
moveFiles('/where/my/files/are/RMA_35200_*.jpg', '/where/they/should/be/';
I'd have to agree with the other comments, "Use glob()to find those files and rename() to move them", etc.
But, there's one thing I would add, a preg_match for the file name. PERL regular expression matching the file name. I think that's what you may be missing from these answers.
foreach ($files as $file) {
if (preg_match('/RMA_[0-9\-_]+.jpg/i', $file) {
...more code here...
}
}

php glob() not finding any files

$directory = $_SESSION['base_url'] . "assets/images/*.jpg";
$images = glob($directory);
This code is not working. If I print out $directory, it matches the directory where I have put some .jpg files. $images just remains empty...
As bsdnoobz answered, this was my solution:
$directory = dirname(dirname(dirname(__FILE__))) . "\assets\images\\"; //escape
$images = glob($directory . "*.jpg");
It looks like $_SESSION['base_url'] points to your app's URL (like http://example.com). You should use filesystem path instead of URL. Try something like this:
$directory = dirname(__FILE__) . '/assets/images/*.jpg';
$directory = $_SESSION['base_url'] . "assets/images/*.jpg";
$images = glob($directory);
There was an additional '.
You have an extra ' between the / and the .jpg

Categories