Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - copy image

i need to copy an image and save in diferent name. how can i do this in laravel?

Please help me.

img/master.jpg -> copy -> paste with different name (user1)
----now------
img/master.jpg
img/user1.jpg
like image 358
Kombuwa Avatar asked Dec 25 '22 21:12

Kombuwa


1 Answers

You can use laravel File helper, here is the file System Doc.

$oldPath = 'images/1.jpg'; // publc/images/1.jpg
$newPath = 'images/2.jpg'; // publc/images/2.jpg

if (\File::copy($oldPath , $newPath)) {
    dd("success");
}

if you need to rename it following would be helpful,

$oldPath = 'images/1.jpg'; // publc/images/1.jpg

$fileExtension = \File::extension($oldPath);
$newName = 'new file.'.$fileExtension;
$newPathWithName = 'images/'.$newName;

if (\File::copy($oldPath , $newPathWithName)) {
    dd("success");
}
like image 162
Kalhan.Toress Avatar answered Jan 03 '23 01:01

Kalhan.Toress