please remember that the complete
information for the class that you are
about to view is at Eli the computer guy
comm not only do we have our videos
there but we have parts lists diagrams
pictures and even complete code examples
so if you are watching this video and
you want more information please go to
Eli the computer guy comm welcome back
as you know I am Eli the computer guy
and in today's class I'm going to be
showing you how to upload files to a
server using an HTML form and PHP so
obviously this can be a valuable
function for any number of different
type of web applications you're going to
create some kind of like photo sharing
application this will allow your users
to be able to upload pictures into some
folder and then your server can then
parse the pictures once they're up there
if you want your users to be able to
upload things such as PDF files or any
of other types of documentation
obviously you can use an HTML form send
the the files to the PHP script and then
the PHP script can then save those files
into whatever folder you decide on your
server so in today's class I'm going to
be showing you how to use an HTML form
and PHP to be able to upload files to
your web server so many times when I'm
trying to teach you folks PHP or other
things in coding I will show you some
simple little function that is actually
rather powerful I'll do a little you
know two-minute video explain how the
function works explain why you should
care show you a couple of lines of code
and that's it that's it then you can go
and play well when you're going to be
uploading files to a web server using
PHP this is kind of on the other end of
the spectrum where you would think this
would be incredibly simple but there's
actually a number of steps involved with
being able to upload files to your
server and if you don't understand how
those steps work it can cause you a lot
of problems when you're actually trying
to go and code your application so
before I go and I show you the
demonstration and the code today let's
go over the whiteboard I guess so I can
sketch out theoretically what is
happening when you're going to be
uploading your your files to your server
so that you
and how the HTML how the PHP how the
directory structure all of that actually
works with what we're doing okay so
let's take a moment just a diagram
what's going to be happening when we're
going to be uploading at the file to
your web server using an HTML form so
the first thing that you're going to
have of course is you're going to have
your standard WW gonna have your web
server and on your web server you're
going to have an HTML form right and so
more or less this HTML form is going to
look like the other HTML forms you've
created the difference being instead of
asking for a user name or an email
address or an age or something like that
this is simply going to be asking for a
file location so basically you're gonna
have a little button you're gonna have a
little box here and basically what
you're going to do is you're going to
click on this button it is going to open
up the file directory on your local
computer you are going to select the
file that you want to have uploaded and
then that path will be put here beside
that you will then have the submit
button what will happen then is with the
HTML form again whatever you're dealing
with a form in HTML you always have that
action you know where are you going to
send the information and so this HTML
form is then going to send the file
information to your PHP script that
you've created at this point this is
where things get just a little bit
confusing so follow along here so
basically what's going to happen is the
information for this file is going to be
sent to the PHP script the PHP script is
then going to parse that information and
then it is been going to try to save the
file into whatever directory and the PHP
file you've specified but before before
it can do that the first thing your PHP
has to do is go over and take a look at
the PHP ini file and ask what am I
allowed to do right so a lot of times
when we're interacting with PHP we can
just use the standard default settings
for PHP and everything will work if you
do echo it'll work if you do Rand it'll
work male by default I think all these
things work most things when you're
using PHP work by default but the thing
is is when you
are dealing with files there are
configurations you can modify within the
php.ini file basically to state whether
you're able to upload files to your
server and if you are there are some
other options there so basically within
PHP diet I and I you have the option for
file upload being on so basically again
from a security standpoint think about
this if you have a normal web server
your users aren't supposed to be
uploading crap to your server you
probably don't want your users to be
able to upload right so for my security
standpoint turning a file upload off
might be a good idea the problem is this
particular project needs you to be able
to upload files so in your PHP ini file
you need to make sure that your file
upload configuration is set to on
there's also a max file size so
basically within the PHP dot ini file
there's a max file size again to say
what's the biggest file you want to have
uploaded to the server using PHP you
want to be a Meg you want to be a gig
you want to be a hundred gigs right
again when you start thinking about real
usability in the real world you do want
to put limitations on what your users
can do because they can do some stupid
things again you're you're coming up
with some clone of Instagram you don't
want your users literally being able to
upload five gig files that would be a
problem or imagine again when you do a
project like this you can put this on a
Xeon server with a massive amount of RAM
a massive amount of storage or you could
you could set all this up on a low note
server right a low note server with I
don't know a gig of storage or two gigs
of storage right you may have some small
virtual machine that you just didn't
give a lot of storage right okay I'm
gonna create this little file upload
server it's gonna be for a couple of
images or for a couple of things
I'll give it I'll give it a couple of
gigs of storage that's all it needs
well your users tries to upload a 2gig
file
guess what you're gonna crash your
server because now your user uploaded at
one time a two gig file your server only
has like two something gigs of storage
in it and then it's going to cause a lot
of problems so one of the things you
have in the PHP Diana
again from a security and from a
stability standpoint is a question of
what is the max file size you want to be
able to upload using a PHP script so
standard is 2 Meg so basically if you go
in there and see the default it'll be
the 2 Meg again for most things for the
default that should be fine you're up
the loading little stupid images that
should be fine txt files fine
PDFs fine but again something to be
thinking about if you try to upload
let's say an image from your iPhone and
you export it from your iPhone it's 10
or 20 Meg's right if that image is 10 or
20 Meg's but the max file upload is 2
then you are going to get a fail out
simply because of this and you may not
you may not realize why that's happening
you may think that there's a problem
with your code when in reality is just
the the max file size 4 for PHP is set
to 2 Meg's so we got that and then we
got to go to the actual file structure
in your server right so your www server
your web server most likely if you're
doing this project is a Linux server so
here you've actually got two folders
you've actually got two folders and
again this is where things can get a
little bit confusing when you sit down a
code so the first folder that you have
to be worried about is the folder or
where you're actually going to be
dumping these files right so a file is
sent from the HTML form the PHP script
and then you need the final destination
folder so in our project we have simply
called this folder because that's how I
go the main thing that you have to
remember is that you have to give other
you have to give everybody the right
permission at minimum to this folder
again we have to think about security
and permissions you have a folder if you
don't if you don't give everybody the
right permission to the folder they're
not going to be able write into it
and the important thing to be thinking
about this though again from a security
standpoint is make sure what folder you
want people dumping files into where
they have the right permission right so
think about this so if you have if you
have the Apache you know you have pack a
web server and you have the default HTML
folder you most likely don't want to be
dumping files directly into that HTML
folder with the right permission you
don't want to give everybody in the
world right permission to the root of
Europe
server right that would be what we call
stupid in the real world so one of the
things are after gonna be thinking about
is basically the folder structure so
with this basically I have the HTML
folder so this is the root of the Apache
server so then basically that's that's
the highest level and in that I created
a folder simply called folder and then I
gave everybody the right permission but
this is one of those things that you
need to be thinking about when you're
coding again thinking about security
thinking about how people are actually
going to be interacting with your
application so on and so forth you may
not even want to put this folder in here
you may want to move it in somewhere
else entirely but we'll deal with that
kind of thing another other projects so
once you have a folder once the other
folder in in the root directory called
something that you've given everybody a
write permission again doesn't
necessarily even need to have read or
execute permission you just need to be
able to write into it important thing
then the other folder that you're going
to be dealing with is your temp folder
so there's actually a temp folder for
PHP and that's actually also up in the
PHP ini folt file and so with a temp
folder for PHP uploads is is basically
what's going to actually happen is the
HTML form is going to send the file to
PHP the PHP is actually going to put the
file into the temp folder so it's going
to put the file into the temp folder
it's then going to figure out a whole
bunch of things about this file again
it's going to go through it's gonna look
for whatever you decide for it to look
for the size is it the right size this
is that the other thing right and then
once once all of that checks out and the
code actually works then what's going to
happen is then PHP is going to move that
file from the temp folder to whatever
the final folder it is where you want
that that file to be stored so one of
the things in the PHP ini file is
there's a configuration for the temp
folder for PHP now that's one thing that
you should be able to leave as default
you shouldn't have to mess with it but
it may be something you may have to muck
with in the future especially if you're
coming in behind somebody so if somebody
else built the web server that you're
dealing with something to realize is
they may have configured that
PHP temple derby up something other than
it should be they may have done
something such as put it into a
partition and the partition is too small
so when you try to upload files and they
try to upload into that temp directory
and a partition too small things fail
out you may have other permission issues
that type of thing so that's one of
those things just to keep in mind so
there's a default there's a default
temple derp where the PHP will put the
file into the default temp folder it'll
figure out what it wants to do and from
there it will move it over to the actual
folder and so yes you have to understand
how this process works
you're going to have the HTML form the
HTML form is going to send your file to
the PHP script the PHP script is then
going to interact with that file based
off of configurations the PHP ini file
again our final uploads even on how are
you allowed to upload files what's the
max final size are you able to do that
and then again if it's set what the temp
directory is and so again that file we
get dumped into the temp directory as
PHP is running through the code once PHP
has figured out everything is okay we
will then move that file from the temp
directory over to the folder and this is
the basic concept of how this works so
with that let's go over to the
demonstration machine I will show you
basically how how this functions in the
real world and then I will show you the
code so here we are at my demonstration
computer again for this demonstration
computer literally all I need is a web
browser and I'm using a MacBook Pro with
Safari but all I knew really need is a
web browser that has access to my lamp
server my Linux Apache my sequel PHP
server so my server is at 10.0 two 1.36
and then I have a HTML form there and
it's called file upload dot HTML
basically all I've done in this form as
I've simply named the form and then here
this is where we queues the file so
we'll accuse the file and then we'll
click Submit so when I accuse the file
this will simply open up my file
structure and the local computer so this
is my local file structure let's say I
want to upload a picture from Italy so I
will upload this particular one we can
now see that image name is right there
and then all I'm now going to do is
click Submit when I click Submit
we'll then send all of the information
to the file upload dot PHP script right
once it's sent the information over
there
I've printed out a few things just to
show you how this works so basically I
did a var dump here so var underscore
dump function it dumps all the
information about a variable so when you
upload a file using PHP all of the
information about that file is actually
stored in an array so what we can see
here is that we have this is an upload
of an array we have a name so there's a
name index about a key for this array
which is image so I mp5 471 is under the
name key we have a type so we can pull
out what the type is it's an image JPEG
we have a temporary name so when we
store this file to the temporary folder
we can actually get what the temporary
folder is and what the temporary name is
here and then we can get the size of
what the file is and so for this
particular file it's 90 2354 so that
means 92,000 kilobytes so basically we
get all of that information in an array
when we upload the file i've simply set
here so if the file uploads all it does
is print out file was uploaded and gives
me what the finished name of the
uploaded file is one of the things I've
also done here is since we get all of
this information an array I can pull
that information out and actually be
able to interact with it so all I'm
doing here is echoing out I'm simply
saying what that information is but
since you now have a values and a
variable you could also do if-else
statements so if this is you know above
50 kilobytes fail out don't allow the
person to be able to upload if this is a
JPEG allow it to upload if it's not a
JPEG don't allow to upload right you got
all that kind of stuff so basically I
have this information here just to show
you that you can pull that information
out you are now dealing with values in a
an array and since you have values in an
array you can then interact with those
just like you would and any normal
so with that let's go over the computer
and I can explain this a little more
from the server side and from the code
side so here we are at my web server
again this is an Ubuntu 18.04 LTS server
I am running the the GUI the desktop
version of this but everything that I am
showing you today should work fine on
any distribution any version of Linux
that you're going to be using and will
obviously work fine completely at the
command line I am using the desktop just
because it's a little easier to take a
look at I did create this lamp server
using a service called task cell or an
application called task cell you install
task cell and then you can call task
cell and then ask it basically to build
the type of server you want so you can
simply say home on a lamp server and it
will install the Apache of them is equal
to PHP it'll do all the default
configurations everything else you need
for so that is how I built this
particular server again that's something
important to understand what things like
the PHP dot ini file your defaults may
be different than my defaults so what
I'm showing you was the default for me
but for you you may actually have to go
in and modify something so the first
thing that I want to show you is just
the simple folder structure again we're
dealing with a patchy web server here
and so in the Apache web server you have
a WW folder and then the HTML folder
this is the root for your Apache web
server when people come to your web
server this is a place that they'll just
defeat by default be dropped here I have
that file upload dot HTML script and
then that is going to pass the
information to the file upload dot PHP
script I then have a folder again I
really have to come back come up with
more creative names for these classes
but anyways you get what I'm coming what
I'm saying this this is the folder that
we were going to be passing at the file
to again we can go in here to properties
and again from the graphical user we can
come here and we can see others others
have the access to create and delete
files again if you're going to be doing
this from command-line and the important
thing is that other has the ability to
write to that particular directory pass
that I can simply open it up and we can
see that image that I mp5 471 that I
guess uploaded is actually now here the
next thing that I want to do is actually
go take
look at the php.ini file again PHP dot
III file is the configuration file for
PHP within the the default the
configuration that I have on my server
the PHP ini file is sitting within the
e.t.c directory the PHP the 7.2 the
Apache Apache 2 and then that's where
the PHP dot ini file is again for your
particular server it may be located in a
different place from this I'm simply
gonna do sudo vim I know a lot of people
hate it when I do this but oh well this
is how I roll and PHP dot ini so super
user do basically wasn't going to give
me the permission to actually be able to
edit the php.ini file and I like vim
again use whatever I don't care
super-secret password 1 2 3 4 5 6 and
enter ok since this is actually the last
place I had it open we actually are at
the the file upload area now again we
can scroll up and we can scroll down we
can see there's a whole bunch of
configurations here
but what you're gonna be looking for is
the file upload sections the important
thing here is that file underscore
uploads is on so if this is off none of
this is gonna work let me be clear this
is off gonna work past that we have the
upload temporary directory so you may
want to set this to something specific
again from a security standpoint from a
resources standpoint for whatever other
reason you may want the temporary
directory to be in a specific place and
so you can set that here you will notice
it's commented out in my particular PHP
dot ini file because I am simply using
the default directory that they gave me
again that gets in Dec gets into higher
administration about what you do or do
not want to do past that we then come
down here to the upload max file size
and this is set to 2 Meg so again you
you decide what you want just remember
while you're doing these demonstrations
again if the rest of this looks fine
file uploads is on and this is this
looks fine I would leave this while
you're playing around with the script
just make sure you're uploading small
files again 20 kilobyte files hundred
kilobit files that type of thing and
then once you figure out what you
actually want to do you can come back
here and modify this is one of those
where many times again if you're going
to be
running a web server in the real world
you are almost always going to increase
this massively but but before you do
increase it massively make sure you
understand why you're increasing it and
understand what risks you you may run
into by increasing it then the next
thing up here is simply the max file
upload so you can actually say how many
files do you want to be able to upload
at one time on a single request and that
is set to 20 which is far more than we
need for this particular project today
so that's the file structure for you on
the php.ini so from this you would WQ so
you would write quick
I didn't modify anything so I'm simply
going to quit and then you would restart
the Apache service again remember to
restart when you when you change
configuration files make sure to restart
services because those new
configurations are not loaded into the
active service until you restart the
service so you can go through and you
can modify a whole bunch of
configurations and if you don't restart
the service then when you go to interact
with the web server it's still going to
be running off of those old
configurations so do you make sure to to
actually go and restart your services
and whatever manner you want to restart
your services so past that let's go and
actually take a look at the code itself
so so again all we have for this is we
have two scripts we have an HTML file
and we have the PHP script so the for
the HTML file this is just normal bland
HTML you should understand basically
everything here so again we open the
HTML we open and close the head we open
the body in h1 I simply give the title a
file upload and then we get to the form
this is where we actually get to some of
the interesting things so form action so
again we need to point this at what's
script we're going to be sending the
information to so it's going to be point
two to the file upload dot PHP
again it's not be pointed to that's
correct method is going to be post again
that's just how it is and then the the
type basically we're going to say a
multi-part form data make sure you don't
misspell anything here real real story
real story I actually had to
troubleshoot this thing for a few
minutes because when I was typing this
out I accidentally did n-type I actually
typed out n-type instead of Inc type
and that was a couple of minutes of
troubleshooting so uh so again feel free
to copy and paste my code and then when
you know it works then go through and
modify it but literally just uh for
getting that see there caused me a
couple of minutes of troubleshooting
anyway so then we're gonna close that
for this all we're gonna do all we're
doing is uploading the file so again
this is a form so you can have a number
of different input types here you could
have a couple of texts so you could say
what is the name what is the description
tags you could have a whole bunch of
different input boxes in this this forum
but for the demonstration today I'm
simply doing the file so input type is
file so basically that's all we're gonna
say it is a file instead of a text or
instead of a number and then we're gonna
give it a name like we normally do and
so we're just going to say upload so
this is just upload I didn't uppercase
you know I guess because then we're
gonna close that and they're gonna do
the input type of submit so again that's
going to give us our little button and
then we are going to close the form so
if we go back and we take a look at our
actual demonstration again this is where
we have the file upload form this is
that input type of file that's what
gives us us so there's no additional
code in here's the no JavaScript to make
this work literally this just functions
from basic old HTML and then there's
this it's the submit button once we
click the submit button then this will
pass all the information to the PHP
script so let's go take a look at that
PHP script and so this is where we have
the file upload dot PHP and there
actually is very little to this I added
some things just for troubleshooting and
just to give you some additional
information so the first thing we're
gonna do here is we're gonna open up the
PHP tag so this is a PHP script the next
thing that we're going to do is you're
going to create a variable for the
directory so what directory what folder
are we going to be dumping this file
into so we do a period forward slash
folder or forward slash so basically we
are going to be dumping these these
files into the folder folder in this
directory so again you would put
whatever folder you've said the name for
the folder there past that we are then
going to create a file name so this
the file name that the file will be
saved as so the first thing that's gonna
happen is the file is going to be put to
the temporary directory and then from
the temporary directory it is going to
be saved into the folder the folder that
we've designated and when we do that we
actually have to give it a name so it's
one of those weird things where
literally what we do is we create the
name for the file from what the original
name was and then we essentially rename
the temporary file back to what the file
was originally called I'll explain it to
you a little bit more when we get down
here
so basically we're doing here is we're
creating a variable called file name the
first thing that we do is there's this
function called base name so base name
basically what it does is it returns the
base file name for whatever you give it
so if you give if you gave a full file
path to a file the base name is able to
grab what just the file name for the
file as it strips everything else out
it's what we're doing here is files so
dollar sign underscore files and this is
an erase when you upload a file using
PHP it gets turned into a ready
information gets turned into a ride and
so upload so files upload this name so
this name corresponds to the name that
you have any HTML form and then you're
asking for what the name of that is so
IMG underscore 0 1 4 7 KP eg that's what
this is right and so basically what base
name function does here is you give it
the name of the uploaded file it strips
out everything other than the pure what
that name is and that's what it returns
we then concatenate on the the dollar
sign directory the the variable
directory so basically what will then
happen is file name is going to equal
dot forward slash folder /img underscore
1 0 for 0 dot jpg right so that's going
to be the full file name and the full
file path we are going to use to save
this file from the temporary directory
into the final directory where we're
going to be dumping it the next thing
that I've done here is I've simply done
the VAR underscore dump so the VAR
underscore dump phone
and what that does is it gives you all
the information about a variable so
again let's go back and actually take a
look at the demonstration here let me
just grab this hit submit let's grab
this oh I don't know hit submit okay and
basically this is what the VAR dump is
so with this var dump here it's going to
get give you all of the information
about the array or in the array about
the file that was just uploaded so
that's what's happening there we go back
we take a look at the code and I'm
simply echoing out break break so
basically just giving me two extra lines
so I can see what the next information
that will be printed out is pretty
easily and then we come down here to the
the actual magic of the file upload so
if so this is an if-else statement so if
we use a function called move underscore
uploaded underscore file and so what
we're going to be doing is we are going
to be moving that file that's been put
into the temporary directory so
basically that when the file
initially gets uploaded it gets put into
the temporary directory and it's given a
temporary name all right so I see here
this name this is the actual file name
so the file name that you gave it when
the file was saved way back when and you
gave it a name
that's the name when you upload the file
initially into the temporary directory
the computer gives it a temporary unique
file name and so what we're doing here
is this function says move uploaded file
from its temporary location to whatever
this file name and file path is so
basically what it's saying is move the
file that you uploaded so this is the
file that you uploaded from the
temporary directory to whatever file
name is so period forward slash folder
/img underscore one zero nine six four
five seven or whatever else right so
basically this is moving the file and
essentially renaming the file back to
what it in ischl II is now it's
important with that whole renaming thing
I've emphasized that a couple of times
we'll take a look at look at that in
another couple of minutes but it's
important to understand so with that
whole renaming thing you can also rename
it to something that you want what I'm
gonna show you in a couple of minutes is
how to add a time stamp to your name to
try to make your whatever file name
you're uploading actually be unique so
past that basically if this works so if
this all works here we're going to echo
file was uploaded and then we're going
to simply print out file files uploaded
and whatever the the original name of
that file was else if it failed we're
gonna echo upload failed and basically
we are going to again give that file
name pass that then we're going to come
down here to the echo information about
the file array and then echo filename
and whatever the file is file type
whatever the file type is file size
whatever the file size is so on and so
forth so that is basically how we print
that information out now again we go
back and we take a look at this so this
file name can cause a problem so again
in the modern world if you're having
many people upload files to the same
server there is most likely going to be
a time when you have two entirely
separate different files that wind up
having the exact same file name so again
imagine if you're going to be uploading
picture files to your to your server you
have people with different cameras but
those cameras the way they save images
the naming convention for saving images
they're all going to be saving image
names the same way and so you can have
two or three or five completely
different images that end up having the
exact same name so when people then go
to upload you're going to run into
issues with your script you may run into
an issue where when you go to upload the
the script basically the file already
exists so the file upload process will
fail out because the person doesn't have
the ability to overwrite or even worse
you have the ability to overwrite and
now basically people are overriding the
files with the exact same name
and then chaos can ensue so one of the
things that you can do is you can add
some kind of unique identifier to a name
so when you save the file out of the
temp directory you can add that unique
identifier so that you you generally
have unique names for anything that gets
uploaded so right here so the IMG
underscore 5 4 4 0 what I'm going to
show you how to do is basically add a
timestamp to that so we add a timestamp
- and then whatever the thing is and
then we know ok then this is a unique
name so we don't have to worry about
either overwriting a final accidentally
or having our script fail out because
the file already exists so let's go over
and take a look at the script itself and
so what we're looking at here is
basically again this is the directory so
we have the directory and that's the
folder and this is how we currently
create the file name so the file name
that we identify down here when we save
the file is base name of the files when
we upload of the uploaded file and the
name so basically this grabs the name
the original name of the uploaded file
and that we concatenate on the directory
and then that's how that file will get
saved
so period forward slash folder /img
underscore five four four zero dot jpg
but if somebody has the exact same imv
underscore five four four zero dot jpg
file name then it's going to cause
problems down the line so one of the
things you can do is you can add
something you can you can use any kind
of functions you want some kind of
unique naming scheme you might use
randomized randomized numbers you could
use the rand function what I like to use
is the timestamp function so I'm just
gonna add a timestamp to this name so I
create a variable called timestamp we're
gonna make it equal to time and so
basically this is going to be giving us
the seconds since epoch so this is going
to give us a nasty long number I
actually really don't care I care a
whole hell of a lot about that nasty
long number what I do care about is that
it should gain or generally be unique as
the seconds click by it's always popping
out with a new number and what I care
about is
new number and so what I'm going to then
do is basically I'm going to concatenate
in this timestamp to this name so what I
have here is that directory so the
directory period and the name so what
I'm going to do is I'm simply going to
add a time stamp in here so I'm going to
it so it's going to be period forward
four slash folder forward slash
timestamps or whatever the timestamp is
and then we're simply going to add on
that name so now we are going to have a
unique name whenever we go to upload a
file then I'm going to hit file I'm then
going to hit save then we're going to go
back we're gonna take a look at our
script and so we go here and again so we
have if we have the I MV 5 4 4 0 jpg I'm
now going to hit submit everything looks
good here from my n so again for me as a
user okay file uploaded IMG underscore 5
4 4 0 j PE key ok so that all that all
looks good for me I don't get confused
but the cool thing is if we go over and
we take a look actually in the directory
what we're then going to see is that I
am V underscore 5 4 4 0
we now have a timestamp in front of it
right so we have a timestamp there so if
we go back and let's say again we just
try to re-upload the exact same file and
then we go back and we upload the exact
same file we can then go and we can take
a look at our folder and we can see that
these are now unique names so for this
example it is the exact same image but
what we can see is we're now naming it
differently so that we then have
different copies so again if if we had
one file name that was being used for
multiple different images or multiple
different files this is a way to give
them all unique names so that they can
simply get uploaded and then I can sit
here and on the script itself I mean you
can see as I click Submit
every time I click Submit you know it's
now adding adding that file there so I
don't have to worry about overwriting in
one way or the other I don't have to
worry about overwriting a file that
shouldn't get over it in and I don't
have to worry about files or the script
failing out because a file can't be over
and so basically that's how the file
upload HTML form and the PHP script work
so now you know how to upload files
using an HTML form and a PHP script at
the end of the day it's pretty easy
again as long as you know all of the
different things that are involved with
actually uploading a file using HTML and
PHP again you have that HTML form so you
have to set that up properly and again
don't don't forget the seat
don't miss type anything will be a
couple of minutes of troubleshooting if
you do but anyways you create that HTML
form that HTML form is going to simply
put the input type to file plus any
other input boxes you want again you
could have 20 input boxes on there if
you want I just did one today you are
then going to have the action the action
is going to point to the PHP script
that's going to parse the information
that you're sending to it so that's
where you're going to be sending the
file the important thing to understand
from that point though is then we have a
couple of other things going on we have
that PHP dot ini file so the PHP ini
file is the configuration file for PHP
you have to make sure that file uploads
are on you have to make sure what the
max file size is again two Meg's should
be fine but maybe it's not the temporary
directory again should be set to default
and again default temporary directory
should be fine
maybe in your situation it's not and
then you have the the max number of
files that can be uploaded at any one
time again that's set to 20 should be
fine maybe for you it's not again
when you modify the PHP dot ini file or
if you modify the PHP ini file do make
sure to restart your Apache 2 services
or your nginx or whatever web server he
were using if you don't do that you're
not going to then bring those those new
configuration files into the running
configuration and the stupidity will
ensue if you do not restart your
services after you modify configurations
that's just a bad day that is what we
call a bad day past that you then are
going to have to have a folder that
you're going to need to dump these files
into again that folder isn't going to
have to have write permission for
everybody in the whole wide big world
right so again you're gonna have to be
thinking about from a security
standpoint
do you want that folder to reside so
again at the bare minimum create a
folder within your your Apache root
directory with the right permission so
that people can dump the files in there
again from a security standpoint
theoretically could could you make your
HTML folder writable for everybody in
the world yeah you can you can might not
be a smart idea though and again that's
where even one of things everything
about from a security standpoint how
you're going to do things once you have
all that set up so you have a folder
with a write to write permission for
everybody in the world you're the
php.ini file configured properly then
basically what's going to happen with a
PHP script is the HTML form is going to
hand the file to the PHP script the PHP
script is going to turn information
about that file into an array so the
size of the file the type of the file
the name of the file the temporary name
of the file all that information it's
going to do that plus it is then going
to put the file into the temporary
directory so you're going to have the
information about the file and the file
is going to be on the temporary
directory from that point you have to
then decide what's going to happen
that's where you use the the upload
function basically move uploaded file
you use that function to then move the
file from the temporary directory into
wherever you want so you have to have
that full file path so basically you
have to have the directory plus the name
you plug all that in you do the FL
statement if it works it's fine it works
printout yeah it works else it fails
print up Bui failed and then go from
there
again when you're doing this remember
you're getting a whole bunch of
information about that file in an array
and so you can then use that information
in further FL statements or previous FL
statements so if the file is above a
size that you want it to be fail out
don't let the file will be uploaded if
the file is or is not of a type that you
want it to be have it fail out so again
let's say you want people to upload
reports or something so you only want
people to be able to upload PDF files so
basically you can have PHP a look at the
type if it is a PDF allow the file to be
uploaded if it's not a PDF say upload a
PDF this
in a PDF uploaded PDF right and you can
do that type of thing we'll talk about
more of that in other classes into the
future but this is a basic understanding
of what's going on again one of the big
things here beyond the php.ini and all
that is do you remember the file
initially gets gets stored into that
temporary folder and has given a
temporary file name a completely unique
temporary file name and so with that
move uploaded file what you're going to
do is you're going to take that that
file that's in the temporary folder and
essentially you're going to be moving it
and naming it to whatever variable
values you have given that's like the
important thing going on here
so with that go play again copy and
paste like a copy and paste my code then
start tweaking it because it is you
forget one thing so I have so little
hair this is why I have so little hair
because I'm trying to teach you folks
all this stuff and so many's little
projects you just make one stupid
mistake you spend an hour doing things
great but you make one stupid mistake
it's been the next 15 minutes figuring
out what you did wrong again that HTML
form curiously enough I forgot to see I
literally I forgot to see as ENC forgot
to see and that was that was about 10
minutes of troubleshooting so uh so do
do make sure you type everything
properly when you're coding or it's
gonna be a bad day with that as always I
enjoy doing this class look forward to
see the next one if you like the content
that I create please think about going
to e line the computer guy comm and
becoming a member or donating please
understand that all the educational
videos are in front of the paywall that
includes the videos that includes the
notes the diagrams and the code example
all of that is freely available and in
front of the paywall but if you want to
watch opinion videos or if you want to
be able to comment you do need to become
a member
membership is $5 a month or $60 a year
and gives you access to those opinion
videos and the ability to comment if you
don't want to become a member you just
want to give a one-time donation there
is also a donate button where you can do
that please understand in order to
provide the education that I am it does
cost money the servers cost money
equipment cost money travel cost money
all of these things cost
reasonable amount of money and the fact
of the matter is is YouTube's
advertising program no longer supports
creators of the way that it used to so
if you want to these classes to continue
to stick around and you find them to be
valuable please think about either
becoming a monthly member or donating a
few dollars for this project