Introduction to Laravel delete file
Laravel is one of the most prominent and effective PHP frameworks that has been dominating the web-development markets over its contemporaries like CakePHP, Codeigniter, Yii frameworks. PHP or Hypertext Preprocessing is currently the most widely used server-side scripting language. Because of it’s clean and easy coding syntax, it became more popular among the web-developers. It is thus becoming one of the most popular languages for backend development. And Laravel being one of its prime frameworks, works excellently for developing secure and scalable web applications. Due to its excellent features such as MVC support, secure authentication systems, fast development time, customization facilities, and most importantly, mail integration services are adopted by a wide range of IT professionals.
Foundation: Taylor Otwell developed Laravel to contribute to the creation of web applications inspired by the MVC( model view controller) architecture. The principles of Symfony were also creating it. It was first released in 2011 with an intent to be a powerful alternative to Codeigniter. Each passing year has truly become an alternative to the previous PHP framework with certain capabilities that the former one lacked and became a reliable MVC framework.
Today, in this article, we will precisely talk about how to delete a file from a Laravel folder.
KIck Start Now: So, if you are at a beginner level in learning Laravel or might be a junior developer and got stuck between how to delete a file from the Laravel folder, then this article might help you. Laravel framework finds its use in building powerful and dynamic websites, especially in e-commerce platforms. Thus, to run and maintain our websites, we often need to upload various files or images in our Laravel folder. After our work has been done, we need to delete them as well. At this point, we have seen many junior programmers struggle with the codes to delete them. On some occasions, it is seen that they successfully deleted the files from the database but worked to delete them from the Laravel folder. Hence, ensuring the occupation of memory with useless files and images. So, while deleting your files from the database, make sure that you succeed in erasing them from your Laravel folder too.
How to delete files from a public folder in Laravel?
In this article, we are going to show you how you can, at first, check if a file exists and then delete the file from the folder. So, if you are a user of Laravel 7.x or 6.x, here is your solution. Here, I got two methods of deleting the files-
- Using the Laravel File system.
- With core php functions like file exists() and unlink().
You may opt-out any of the ways whichever may seem suitable for you.
1. Using the File system
Code:
public function removeImage()
{
if(\File::exists(public_path('upload/bio.png'))){
\File::delete(public_path('upload/bio.png'));
}else{
dd('File does not exists.');
}
}
2. Utilizing Core PHP Function
Code:
public function removeImage()
{
if(file_exists(public_path('upload/bio.png'))){
unlink(public_path('upload/bio.png'));
}else{
dd('File does not exists.');
}
}
The above solutions are convenient with the Laravel 7 framework. They will help you to wipe out the unnecessary ones from your directory, whereas for Laravel 6 users, we have another solution to shoot your problem.
For Laravel 6, we are using the delete Directory() with a file object to delete the directory. In this, we can get the deleted folder path, and after that, we can use our get folder path in function. If it prospers, it will return true, and upon failing, it will return false.
However, we would like to recommend creating a folder named test or demo before executing the program.
Examples to Implement of Laravel delete file
Below are the examples of Laravel delete file:
Example #1
Code:
public function index()
{
$folderPath=public_path('test');
$dd =File::deleteDirectory($folderPath);
dd($dd);
}
Here, if it returns true, then the method is a success, but if it returns false, then it fails.
As for Laravel 7, we have given examples using both Laravel and PHP; likewise, we are going to give an example using the PHP function too for the Laravel 6 users. Here it is,
Example #2
Code:
public function index()
{
$folderPath=public_path('test');
$dd =rmdir($folderPath);
dd($dd);
}
Likewise, the above one, if it returns true, then it succeeds, and if it returns false, the method fails.
Also, we have come across people saying that while trying to delete the files from the public path using file:: delete, they often get the message “File not found at the path.” For them there is our answer:
Solution for laravel
Code:
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index(Request $request)
{
$file =public_path('img/slider.jpeg');
$img=File::delete($file);
dd($img);
}
If returns true the method is a success otherwise it fails.
Solution for core PHP
Code:
/**
* Show the application dashboard.
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function index(Request $request)
{
$file =public_path('img/slider.jpeg');
$img= unlink($file);
dd($img);
}
Like the above ones, if the output is true, the program succeeds, and if returns are false, then it fails.
In Laravel, the delete method accepts a single filename or an array of files to remove from the disk. Suppose a text file is saved in my public folder of Laravel, which is no longer a requirement. we want to delete it from my saved controller. we also want to use a specific link to eradicate the file: link(‘public/test.txt’). To avoid error, you have to use the unlink function of PHP. Also, keep in mind not to forget to create the complete path of your respective file if it is not stored from the beginning.
Conclusion
As discussed earlier, Laravel is one of the famous frameworks of PHP due to some of its incredible features. Unlike its contemporary code igniter framework, it comes with built-in authentication class features and unit testing facilities. It is what makes it different from the others. Also, it’s open-source facility facilitates the users to avail of the benefits free of cost, unlike CakePHP. In the case of data backups too, Laravel would take a hands-on approach over the others. Assembling all these, we may see there are enough reasons why one should choose Laravel instead of other PHP frameworks.
Recommended Article
This is a guide to Laravel delete file. Here we discuss the Introduction to Laravel delete file and its different examples along with Code Implementation. You can also go through our other suggested articles to learn more –