Laravel File Management: Display and Delete Files From Storage

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome back to programming fields in the last video of larel coding playlist We implemented file and image upload functionality with validation and we covered multiple file uploads as well also we understood the different functions provided by larel to a store file such as a store a storage a store publicly and move so if you are new to larel then I recommend you to watch that video in order to continue with this in today's tutorial we are going to save the file name into the database and thereafter I will display all those uploaded files in a view and also we will implement the file delation functionality from the storage this is a very common scenario in web development and larl makes it incredibly easy to handle so before we get started don't forget to like this video subscribe to the channel and hit the notification Bell to stay updated on our latest tutorials so let's jump right into it so till now we have completed the file upload function ity in larl so for that we have used this file validation and we have checked how we can upload the single and multiple files so to upload file and image we have seen a store and a storage function along with the move okay so the uploaded file and image are stored in this uploads folder if we will look into this storage app and inside this we have this uploads folder and we used file system so by connecting this storage link these uploaded files and folder are linked inside this public folder if I will navigate to the browser then we had this UI so let's check the example by uploading another file so here I'm going to upload one PDF and if I will upload you can see this file is uploaded okay and we will able to see the uploaded files inside this storage uploads directory so now here instead of a store I will use a storage function as we required a file name and and uh I will specify file name and I will move this file name variable I will simply uncomment it and instead of this request file from Dollar file so we are going to upload this multiple file so whatever files will be coming from this form request here those files will be iterated and based on the single file I will get that extension and here I will add a new file name by adding time stamp so that the uploaded files will have a unique name and here we will store the file name inside the database okay so in order to store the file name inside the database we required a model and migration so switch back to terminal and here we will hit enter HPR make model and we will provide model name as file and also we need to create one migration so that we can store the file name so here I have passed one flag as hyphen M if I will hit enter it generated two files model and migration ation we already have the file controller as we created okay so now let's switch back to migration and here we will create column name as string and here we will provide file name okay and uh pass as n label also we will store the original file name just like file original name okay and also this will be n level that's it as of now I'm storing only these two Fields as file name and file original name now we have to migrate this table inside the database so that this table schema will be created so we already created our database connection so let's migrate this table we will enter hpan migrate so the migrations are dumped into the database and we have this scheme as created if we will check out to the database and if I will refresh and here we have the tables created now let's come to the code and as per this migration we will have to create fillable data inside file model so we will create a protected doar flb equals and here we will pass file name and the second column we have file original name yes so simply copy from here and we can add it so we have added this model eloquent failable property now let's come to the controller and from here we will store the file name after uploading it inside this folder so this functionality is written to upload multiple files we will add the file save functionality inside this Loop so when the file will be uploaded file name will be stored inside the database yeah so after this line uh I will simply call file model as we have created and I will call this create function and inside this create function I will pass attribute as file name and this file name will be time stem that we have created using this extension so I will pass file name just like this and for the next attribute I will have to pass the file original name okay in larel we can get original file name using get client original name just like this okay now at the name of space bar we will have to import this model and here we will add use app SL models and file yeah so we have imported this file model and here we had captured this response so this is not required here that we will add here means if the file entry is created inside this database then we will capture this response and uh these are not required as of now so I will just remove these lines and yes now let's come to the browser refresh this application and let's try to upload the file yeah so this file is uploaded and if I will check to the database and here inside this database the uploaded file detail is inserted so we have captured this file name and file original name now let's try to upload more files okay so I have selected this three files now let me try to upload again yes so all the files are uploaded again if I will refresh to this table Yeah we have now four files okay this is fine we have uploaded the file and uh we have created those file entries inside the database now in the next step we will list out the uploaded files and we will display in the form of a table and from there we will implement the delete functionality from the storage folder so let's come to this views folder and here we will create another file as files. blade. PHP and again we required bootstrap 5 so we already created that here so I'm going to just copy and add it here I'm not going to create a master view as of now that we already discussed in our laral interview preparation Series so as of now I will simply create one body yes here again I will create one container okay one title as uploaded files and here I will create one table as table right okay inside this table I will create one table header as head and T body that's it inside this T head I will create a th asell just for serial number and I will duplicate this file name and I will add action just to delete now let's render this VI so inside this file controller I will create another function as public function files okay and here I will return view as files that's it okay so the function is created now in order to render this function let's come to the route so here we will create another Endo just like route colon colon get files list okay so here I will have to pass the second argument as function name as we created the function name as files so we have this Endo now let's copy this one and come to the browser and here let's access this one okay so here we will have to fetch the uploaded files from the database so for that let's create the functionality so again switch back to controller and here we will fetch the uploaded file as file colon colon all all right and here let's capture this in dollar files and here inside this compact we will need to pass this uploaded files so that we will be able to capture these files inside this view so now let's come to this View and here we will capture the files so let's use or Els Loop so here we will pass dollar files as dollar file and inside this forls we will have to create TR and TD okay so inside this TD we will pass file file name similarly for this index I will simply uh replace this with ID that's it and here we will have to create action button and here we will simply create one delete action all right I will pass class as BTN BTN danger and BTN SM okay and inside this empty block I will create one empty TR and TD just like TR and inside this TD uh I will have to pass call spin as three and I will add text danger no files found yeah that's it and here we can give a space from top and bottom adding and I will pass one class as font with bold all right and I will wrap this table inside table container okay and I will give border now let's come to the browser and refresh it so here we have the list of files and along with we have this action button so here I want to display the original file name instead of this time time file name so let's change this key name file original name okay and now in the next step I want one anchor link for these files so whenever I will click on that link that will open the actual file okay and that file path will be from a storage path that we have used to upload the files so these files are uploaded through the storage okay and here these are the symbolic links the actual files are stored inside this storage folder inside this storage we have this uploads folder so let's create hyperlink and wrap this file name inside that and inside this TD I will create one hyperlink as just like this here I will close it so we have link this storage folder inside this public folder so now in order to access any file or folder from this public folder we can use simply asset so by default this asset function will point out to this public folder inside this public folder we have this uploads folder so we will have to write uploads and after this uploads we will have to pass SL and we will have to concatenate the time stamp file name okay so just write file and file underscore name now let's come to the browser and if we refresh and if we will click on this and it open the file in this tab here so here we have one issue with this file name in case if you will upload multiple files at the same time then here this will generate the same timestamp F name for all the file because all those files will be uploaded at the same time so in this case there might be confliction with the file name so we can append a few more parameters just like we will use Rand function divided by HP inside this Rand function we will pass minimum and maximum sequence so the minimum will be 1 one this maximum will be99 you can pass anything inside this one now let's come to the browser and here I want to create one button so that when I will click on that button I want to link that file upload form directly so every time I won't have to uh redirect back to that URL manually okay so let's add one button here just after this title here I will add one hyperlink and I will pass uh the route as file and here I will provide name as upload file and here I will pass class as BTN BTN primary that's it now let's refresh and uh yeah route is not defined so let me check this route first now give a name here just like file do create now let's come to this Le file and here I will just pass file do create save it and refresh yes now you have to to float this button to the right so just add float end yes so whenever I will click on this upload file it redirected back to this file now let's come to this delete functionality so whenever I will click on this delete I want to delete the uploaded files from the storage folder also saved file name from this database so let's create this functionality here I will wrap this in a form that I can use delete route so I will transform action as rout do destroy and here method will be post and uh I will use the csrf token method directive inside this method I will pass this delete method and here instead of this hyperlink I will change this to button type submit okay and uh this button will be closed instead of this hyperlink here we'll have to close this form okay and here we can use one JavaScript confirmation as on click equals return confirm are you sure okay now let's define this route inside this web.php so here let's create one lit route as destroy here we will create one function as destroy and I will provide the name route as do destroy so let's create this function inside this controller function let's create this function destroy and here we will capture the file ID so let's receive one parameter as ID and just put DD as dollar ID and from here we will pass argument as doar file okay this is done we have not passed any argument inside this webp HP out let's pass one parameter as ID let's go back and refresh this page let's try again yes we received the ID now let's implement this delete functionality inside this file controller so initially we will find that from the file model means based on this primary field ID we will search for the record from the files table so let's use find function provided by lar elect okay we will capture this in a file if we will search we will add one condition there is file then we will delete the file from a storage okay so larel provides a storage facade in order to handle with the storage thing so we will use this storage colon colon delete function okay and this delete function accepts one parameter so here inside this delete function it accepts the file path along with the file name so basically this storage refers to this storage app directory inside this storage app we have this uploads folder so firstly we will have to pass the folder name as uploads and here we will concatenate the file name so the file name is coming from the database as we stored the file name inside this database table so if you will see here we stored the file name inside this files table Yeah so this is done now we will have to import this storage facade at this name space bar so we will write use illuminate support facad and we will have to write a storage yeah this is done and after deleting this file I will have to delete that record from this database as well so this is done and At Last I will return back with success and uh I will pass one message as file deleted okay and uh we will have to capture this response inside this files blade so we already added that alert success response inside this file upload form so better as of now we can simply copy this from here and uh I will add it just after this title yes this is done now let's come to the browser let me go back and if I will refresh it now let me just try deleting this first one if I will click okay yes this file is deleted and if I will check that file inside this storage then yes from this uploads folder that file is deleted as of now let me just delete all these uploaded files from this storage and from here as well so as of now we don't have any file inside this storage folder it is completely empty okay upload few files so I have selected three files now let me upload these files yes so the file is uploaded if I will check to this uh directory then yes inside this uploads folder we have these three files uploaded and let me check in the public folder so inside this public uploads yes we have the same files now let's come to the browser and uh yes so if we refresh yeah we have this three and now let's try to delete one so the first one if I will try to delete yes this is deleted and the file is removed from this storage folder as well yes let's try to delete the third one yes the third one is deleted and that file is removed from this storage as well I hope this tutorial will make sense for you so now we are at the end of this video and we have successfully implemented file save display and delete functionality in our laral application I hope you found this tutorial helpful if you have any questions or run into issues feel free to drop a comment below don't forget to experiment with the code and adapt it to your projects if you enjoyed this video please give it a thumbs up subscribe to programming fields and hit the notification Bell for more larable tutorials thanks for watching and see you in the next video Until Then happy [Music] coding
Info
Channel: Programming Fields
Views: 247
Rating: undefined out of 5
Keywords: Programming Fields, programmingfields, laravel, laravel tutorial, Laravel File Management, Laravel Storage Tutorial, Display Files in Laravel, Delete Files in Laravel, Laravel Filesystem, Laravel Storage Best Practices, Laravel File Handling, Laravel Storage Cleanup, Laravel File Upload and Delete, Laravel Storage Optimization, Laravel File Explorer, Laravel Storage Tips and Tricks, Laravel Storage Management Tutorial, Efficient File Handling in Laravel
Id: gFjKsl6Jc10
Channel Id: undefined
Length: 19min 18sec (1158 seconds)
Published: Sat Dec 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.