|
Home >> FAQs/Tutorials >> PHP Script Tutorials and Tips >> Index
PHP Script Tips - Uploading Files to Web Servers
By: FYICenter.com
Part:
1
2
3
A collection of 12 tips on uploading files with PHP pages. Clear explanations and tutorial exercises are provided on creating file upload HTML tags, setting encoding type on HTML forms, getting uploaded file information, storing uploaded file in database.
Topics included in this collections:
- What Is File Upload?
- Which HTML Tag Allows Users to Specify a File for Uploading?
- How To Write the FORM Tag Correctly for Uploading Files?
- How To Get the Uploaded File Information in the Receiving Script?
- How To Process the Uploaded Files?
- How To Move Uploaded Files To Permanent Directory?
- How To Detect File Uploading Errors?
- Why Do You Need to Filter Out Empty Files?
- How To Create a Table To Store Files?
- How To Uploaded Files to a Table?
- What Are the File Upload Settings in Configuration File?
- How To Get the Technical Specifications for File Upload?
What Is File Upload?
File upload is Web page function which allows visitor to specify a file on the browser's system
and submit it to the Web server. This is a very useful function for many interactive Web sites.
Some examples are:
- Web base email systems for users to send attachments.
- Forums that allows user to submit pictures.
- Web sites file managers for users to build their own Web pages.
Which HTML Tag Allows Users to Specify a File for Uploading?
To present an input field on your Web page to allow users to specify a local file to upload,
you need to use the <INPUT TYPE="FILE" ...> tag inside a <FORM ...> tag. The <INPUT TYPE="FILE" ...>
will be displayed as a text input field followed by a button called "Browse...".
Users can either enter the full path name of a local file, or click Browse button to go through
a dialog box to select a file interactively. The following PHP code shows you a good example of the
file upload tag:
<?php
print("<html><form>\n");
print("<input type=file>\n");
print("<input type=submit>\n");
print("</form></html>\n");
?>
If you copy this script to PHP file and test it on your Web server, you should see
a file upload field.
How To Write the FORM Tag Correctly for Uploading Files?
When users clicks the submit button, files specified in the <INPUT TYPE=FILE...>
will be transferred from the browser to the Web server. This transferring (uploading) process
is controlled by a properly written <FORM...> tag as:
<FORM ACTION=receiving.php METHOD=post ENCTYPE=multipart/form-data>
Note that you must specify METHOD as "post" and ENCTYPE as "multipart/form-data" in order
for the uploading process to work. The following PHP code, called logo_upload.php, shows you
a complete FORM tag for file uploading:
<?php
print("<html><form action=processing_uploaded_files.php"
." method=post enctype=multipart/form-data>\n");
print("Please submit an image file a Web site logo for"
." fyicenter.com:<br>\n");
print("<input type=file name=fyicenter_logo><br>\n");
print("<input type=submit>\n");
print("</form></html>\n");
?>
How To Get the Uploaded File Information in the Receiving Script?
Once the Web server received the uploaded file, it will call the PHP script specified in the
form action attribute to process them. This receiving PHP script can get the uploaded file information
through the predefined array called $_FILES. Uploaded file information is organized in $_FILES as
a two-dimensional array as:
- $_FILES[$fieldName]['name'] - The Original file name on the browser system.
- $_FILES[$fieldName]['type'] - The file type determined by the browser.
- $_FILES[$fieldName]['size'] - The Number of bytes of the file content.
- $_FILES[$fieldName]['tmp_name'] - The temporary filename of the file in which the uploaded file was stored on the server.
- $_FILES[$fieldName]['error'] - The error code associated with this file upload.
The $fieldName is the name used in the <INPUT TYPE=FILE, NAME=fieldName>.
How To Process the Uploaded Files?
How to process the uploaded files? The answer is really depending on your application. For example:
- You can attached the outgoing emails, if the uploaded files are email attachments.
- You can move them to user's Web page directory, if the uploaded files are user's Web pages.
- You can move them to a permanent directory and save the files names in the database, if the uploaded files are articles to be published on the Web site.
- You can store them to database tables, if you don't want store them as files.
(Continued on next part...)
Part:
1
2
3
|