In this Laravel tutorial, we will learn how we can copy files from one folder to another folder.
file copy required many times when working on the project which is a very common activity and one should be aware of it that how we can do it.
Sometimes we need to copy the files from one location to another in the project. In Linux we have done so many times but what if it is required to do with Laravel. So today we are going to make you understand, how you can copy the file from one location to another location using laravel.
And If you are familiar with Linux then there is a command cp
to copy files from one location to another. In Laravel, we can achieve it by using copy
method with File
or Storage
Facade.
Copy file using File Facade
You can simply use the below syntax to copy files from one path to another, you just need to replace the from_path and to_path with their actual path
File::copy(from_path, to_path);
Copy file using Storage Facade
We can copy files from one location to another using Storage Facade also. Below is the code for the same.
Storage::copy(from_path, to_path);
Let’s suppose you want to copy the file ‘file1.jpg’ from the old directory to the new directory then you can use the below line of code
Storage::copy('old/file.jpg', 'new/file.jpg');
Hope the above article helps you in solving your query.