hi everyone welcome back to the
level 7 tutorial in this video we are
going to learn about
creating zip file and download so let's
see how we can create and download zip
file in laravel 7
so first of all create a new controller
so switch to the command prompt and here
just to stop the running application and
just create a new controller so just
type here
php artisan make
controller and let's say controller name
is
zip controller
okay now press enter controller created
now run the application so just write
your php artisan
inside okay
now switch to the project and just open
zip controller so go inside here app
directory
then http controllers and
you can see here the zip controller so
just open it
and here just create a function so
just write here public
function and function name let's say
function name is zip
create and download
create and
download okay
this is the function name and inside the
function
just right here zip is equal to
new zip our chip
okay now just import this
z party package so just right here
use deep rgb
all right now create a variable for
zip file name so just write here file
name
is equal to let's say zip file name is
my zip
arrow open and
here i'm just going to
use public directory so just write here
public controls for
path and
inside
just pass here this file name so just
write here
dollar
file name okay
and
and is equal to
all right inside this if create a files
variable
and file colon colon
files
public underscore path
and here just
let's say folder name is my files
okay now use this file
so just right here
use file and
let's create this folder name inside the
public directory so just
go to the project directory and go
inside the public
and here just create a new folder
and folder name is my files okay
now just open this directory and here
just copy some file
so i'm just going to my desktop
and from here let's copy this file
okay now just copy
and paste inside the
public my files directory okay
all right
my files and this file
all right now switch to the project and
here
uh just write for each
dollar dollar files as
dollar key is equal to
uh arrow sign okay right arrow
dollar value
and dollar relative
name is equal to
base name
and pass here this value
okay dollar zip
and simply add the file to the zip so
just right here i wrote add files
add file and dollar value
this one and just pass in the relative
path
relative file name okay
relative names
uh after the forage just close the zip
so zip arrow close
okay and here just
simply return a response
return response
and download
okay and
public underscore path
and just pass share the this zip file
name
okay
all right now save the file
and let's create the route for this
function so go to the web.php file
just go inside the routes directory then
web.php
and here just create a route
route to get
let's say uri is zip
okay and controller name
which is zip controller and create
function name
let's copy this function name zip create
and download
all right now save the file
now let's check it so switch to the
browser
and here just add zip
okay now press enter
you can see here zip file is downloading
and just open it
all right now extract the zip file so
right click
and then extract files enter
and you can see here all files
okay
so this way you can create and download
the zip file
laravel 7 so that's all about creating
zip file and downloading so thanks for
watching this video
and we will still have any kind of
question you can ask me in the comment
box
don't forget to subscribe my channel
thanks for watching