Interacting With The User / Uploading a File pt. 2
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
Now as well as the user file variable another set of variables automatically passed on by the html page and these are user file, name and this is the name under which the user file appears on the users computer, on their hard disk and then the size, which of course is the size of the user file. These variables are automatically generated when the html page submits the data which is why you can’t see the place where they came from. User file type, that will specify if it’s text type or jpeg or what kind of type the file is but at the moment we’re just dealing with the user file variable and if none is sent then this value is automatically generated. We give an error message here and say ‘no file specified’ and exit, then we’re going to use a command called ‘move’ uploaded file. This is a php function, which allows us to send the user file to a place that we specify in the second argument and this we’re going to specify a directory and preferably this should be outside of your directory of your web server root directory so, if we take a look on our hard disk here I’ve created a directory called uploads and that's just simply off the root of c, that's not within our document root where we've been putting everything else here. So we’re simply going to put the files into this uploads directory. The way we specify that is our backslash to specify the root and we'll need 2 back slashes because to make sure that the back slashes are interpreted correctly rather than just the escape character which is what php will assume it means and we’re going to put in the name of the directory there. I should add that we’re using 2 back slashes because we’re running this on a windows machine. If we were to run this on a Unix machine, we would be using a single forward slash. So there’s the directory and then we put the original name that the file had when it was submitted. Now at the moment, this is simply a command or statement, however we can check that this is been correctly carried out but enclosing it within an ‘if’ condition. Now since every function, every statement in php evaluates either false or true or some other value, this one is no exception. This one will evaluate true if the file is moved successfully and will evaluate false if its not. So we can put another couple of messages in here with an ‘else’ statement to make sure that the file has been moved correctly. So let’s save our file and we'll try that out in the browser and our file was called submit file.html and here is our upload page Now if we brows for, let’s find some text file of some kind, let’s say we use this password file and as a user, we’re going to send our file to the web server. Your file has been uploaded it tells us. Now let’s take a look and make sure that it's really been sent. We'll have a look at uploads and sure enough the file that we selected has been moved by php to the folder we specified. And I have to end this movie again with, by repeating my caution that file uploads should only be allowed once we’re satisfied that first of all the user is someone that we trust and also we should make very sure about where the files are going.
Tutorial Information
| Course: | PHP |
| Author: | Joshua Mostafa |
| SKU: | 33332 |
| ISBN: | 1889347787 |
| Release Date: | 2002-03-26 |
| Duration: | 9.5 hrs / 92 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 