Laravel Storage | let's learn storage by creating a simple file explorer simulator

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in this video we'll learn more about file storage envelope so let's start here I will perform all of these operations in my endpoint but before that let's take a look at file systems config here this file systems inside config folder right as you can see here we have something called disks which means where will we store our files so locally it means we will store it in our local server or our own server and the public also uses local but this public means all of our uploaded file will be public but here by default it's private and not available for everyone right so if you want to make all of your files public you can use public directly right and here our default file system is local as you can see here you can change it to public or any value that's here inside our config file also you can find it here in our DOT in the file right and here for key file system driver or file system disk is one is local you can change it to public if you want we will keep it local and if we back here to our code I will set it again to local right and here is S3 it's related to Amazon we will talk about it later also you have here symbolic links we will talk about it later in this video also so now here in our controller let's create a new folder but if you notice here inside our disk local disk the root storage folder is app so which app as you can see storage app pause so let's run our method I will sort my server first and I will run BHP Artisan thinker to see what's the value or what this search pulse method returns so here storage path and let's see here is our full server or full path and inside our project it represents the storage file here our project and here inside our storage folder so this one and the storage box here is this folder is app folder right so if we back again here and we pass this value app it is storage slash app if you found or post anything here it written is storage slash app right so let's start here and I am using the local disk as a default this one and let's create a new folder to create a new folder or directory we can make it easy using storage facet or class here and say make directory for example laravel boy right now let's check it returns true right return here to return one back to our end point and let's test it here send the request and back to our storage folder here this one app inside app it's created a laravel boy folder right now let's start uploading a file or storing a file into this folder so we can do it easily using here storage Right Storage this one put file we can upload file using pod file method and I will say using laravel boy folder store this file we will define it soon and here I am uploading a file using Avatar and I set it to file if you want to upload file using Postman and I will select an image image one this image right and here I will Define my variable file equal to request file here and Avatar because our name here is Avatar as you can see here right so let's back and test it here to our endpoint as you can see our laravel folder is empty and when we try to upload a file it's here return it must be returned something here return because put file returns string or false if it failed so I think it must return the pulse of that folder we sent a request already and as you can see here it saved the file here our file and renamed it also as you can see here if you want to rename this file you can use this method put file as and as you can see it takes 3D parameter the bus you want to save your file or directory the file can be file class or upload file request like this one as you can see it returns upload file class and the third parameter is the name you want so I want to name it here let's create a variable called name here name equal to for example Ahmed and Dot file extension this one file extension right and don't forget to add dot here also it will be amma.png or the extension we have and here is the new name I want right and as you can see here we have something called options we can set the visibility of file public or private by default as we know it's private but we can use public maybe and we can use also instead of that public disk so I will not set it to public back again and you try to create the same image send and it Returns the pulse of my images inside a lot of it by folder slash arma.png it's back and control here our new file ahmad.bng what if we retry to create another image or file with the same name let's check now send and as you can see it's overwritten so you must create unique names you can actually use it using hash name as we explained in the previous lesson using here generated name right equal to file hash name method this one and if we die on the bit generated name it will be generated by laravel send request and here is our generated name dot PNG and you can save it directly to our database right what if we want to copy a file to another directory so for example here I want to copy this Ahmed to PNG to public directory it's easy we can use here storage and copy from Ahmed or sorry laravel boy slash ahmad.p in G this file to public slash ahmed.png don't forget our root storage folder is this one here is our app as it's mentioned here right so back again and now it returns success back and see inside public as you can see it's copied also and it's kept here right from Blood of boy I create another copy I'm a do BG inside public folder let's completely move a file from here from a lot of the voices file I will cut it from here to public right we can make it easy also using storage move and this is the same parameter let's copy it and the same thing it will be overridden if it's already exist so back again send the request it returns success and back to our storage folder inside laravelboy it's deleted or cutted to public folder as you can see here ahmad.png right the next one here is listing a file inside directory or listing all files inside the directory the first thing here we can let's take the name here and we can say storage and files inside public folder so list me all files inside public folder if we die and dump here by the way this public folder which is exists inside the storage folder app public this one so it must return ahmad.png and this is in the request it returns dot get ignore as you can see because we have dot kit ignore file and our folder here public slash armor.png what if here this public has another subdirectory like that for example I will create this one here and sub there and I will copy it here to sub there with another name test for example right back here and refresh it returns only the files inside main path but if you want to inside all files inside sub directories also you can use all files method so it will display files inside public and also files inside directory inside public backend see and here's our Public Sub there slash test.png right so here if you want to show a file or getting the content of file you can use method here let's return this one storage and get we want to get the content of tested PNG or ahmad.bng so public slash ahmed.png and I want to return the content of that if you understand the string so if we test it here it returns this image right right so we we can use this file content right to create another content so for example I want to create a new image so we can do it easily using storage and using put method so inside our laravel slash boy and slash I want to create a new file using test2 dot PNG and create it using this file content a copy using the string of the content here so we can use file and let's try to test it here return I think it returns Boolean yeah try send request it's already succeeded back to our laravel boy here and it created the same string as you can see here from this file content so here is another way for example to create a copy using put instead of copy right if you want to download file using storage we can do it using storage but we must return right so here storage download method and let's specify our image or our file not only images by the way and here we can say this one and we can also select the name while downloading and specify headers right because it's a type of response so we can do it easily like that if I try to download it using Postman from here select send and download now here is our test2.png as you can see here is downloaded to my desktop or download folder right now it's time to delete some files we can here do like that storage delete right so here I want to delete this image or this file here and return to retain success or not it returns Boolean also I think yeah as we can see here send it for the first time it returns success one and if we retry to delete it and it deletes the file if it exists and if not exists it didn't throw any error so instead of that we can first check if file exists or not so F here storage using exists method to check if a file exists or not here this test to PNG because I removed it already if this file exists then delete it right if not we can say return no file back and descent again we have no file because we already deleted it if you want to delete a folder we can use this method storage delete directory this one and we can specify our directory so I want to remove this level boy folder and it will be removed with with its content so if I back here to my folder larvalboy it has an image and if I run it this one return run here it works run again it works also because it doesn't exist anyway it will success right here's our laravel boy folder has been deleted so to show a file or to make a file public we can use this public folder or even you can make your own custom folder so let's first clean it here quickly so I want to upload a file to our public folder and display it but let me show you something here so we can store file directly using store method right or so biblically it will stored directly inside public and store public as you can rename it and let's change it to store here and save I want to store it inside public folder and return here is this one uploaded equal to this one get the UN dump Swan storage URL to get the URL of a specific folder or file inside public here slash the name this one let's define our name here name equal to file hash name which will be generated by laravel and let's check send our Avatar again and here is our folder storage slash PNG but if we use this URL here right let's duplicate this endpoint ttb and if we copy this storage it returns nothing it is 404 because it's public but it's fake public so we must create a symbol link here if you notice here this one creates a links to make file or folder public to show so we can make it easily using this command estimated here storage link so artsun pitch Watson storage link right and here in our public folder this one here we have now storage folder right with these files so let's try again now it Returns the image because it's now saved as publicly if you want to create your own custom links here we can do something like that so let's duplicate it and we can say for example create Avatar inside public folder right or avatars for app public or app avatars folder inside storage right but we must have this avatars folder before re-run this Command right so first if you want we must unlink this storage folder from public folder right so let's remove it and let's create another folder called avatars here avatars inside our storage folder and inside app also and created our doors right and if we run it again storage link now it created two symbol Link Storage and avatars let's upload some files to avatars instead of public right so here store as and the storage inside avatars or just use store and here is also our avatars and let's back and send it again upload file Now it's uploaded let's try to show it publicly here and it's not exist why because we must return the asset of that image as a theorem so we can do it easily using asset here right let's take it and use this one return this the simplest line also let's disable it and here is a folder of Avatar that I uploaded so copy paste and here is the result so you can also create your own custom symbol link right here is some general explanation about storage file and larval support me by clicking on like share and subscribe see you in next video
Info
Channel: Laravel boy
Views: 9,478
Rating: undefined out of 5
Keywords: Laravel, learn laravel, php, laravel boy, advanced laravel, laravel tips and tricks, laravel tips, laravel advanced tips, learn php, laravel, PHP, web, laravel helpers, laravel custom helper methods, master laravel, laravel route, controllerless route, quick route, one line route, laravel boy route, laravel tutorials, laravel download file, laravel display file, laravel secutriy, laravel store files tip, laravel storage, all about storage, laravel file explorer
Id: m9CSLR8EGzM
Channel Id: undefined
Length: 19min 38sec (1178 seconds)
Published: Wed Dec 28 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.