PHP file upload_php tutorial

Source: Internet
Author: User
Tags php file upload
Php file upload. For php Tutorial file upload, you can use PHP to manage your Upload. First, you need to build an HTML form file to be uploaded as the user interface. There is an example in the following to show the appearance and save a php Tutorial file to Upload

You can use PHP to manage your uploads. First, you need to build the text uploaded as an HTML form on the user interface.

. The following example shows the appearance and saves an edited HTML code.





Some rules need to follow the HTML form during construction. First, make sure that the form uses the POST method. Second, shape

Type requires the following attributes: Character encoding = "multiple/form data ". Information extraction used when the specified content type is used

To the server. Without these requirements, your file cannot be uploaded.

Another thing to note is that the hidden form field name is the value set by MAX_FILE_SIZE. Some Web browsers

This field does not allow users to upload files larger than this number (in bytes. You should

Set this value to the maximum upload size in the php. ini file. This is a set

Upload_max_filesize. the default value is 2 MB. But it still cannot guarantee that your script will not be transferred to the ruler.

Large files. The danger is that the attacker will try to send you a request for several large files and fill in the text

File system, that is, the PHP storage and decoding file. Set the maximum number of instruction files in the post_max_size command file of php. ini.

Size, which must be greater than the medium upload_max_filesize ). The default value is 10 MB. The

It is required that the maximum size of POST data be allowed. In addition, make sure that your php. ini file

File_uploads is set to On.

At least one of them can be viewed as "type =" file "in the input tag attribute ". It is used to specify the control input for the file selection

Element. This provides the URI of a file, you need to enter a "browse" button, which can be used as an alternative

URI input.

When the user enters the URI of a file and clicks the submit button, the copy of the file will be sent to the server and the user will

Redirected to upload. php. This PHP file processes form data.

Back to top

Process form data (PHP code)

When the file is uploaded and PHP creates a temporary copy of the file, and creates an ultra-global variable $ _ FILES array,

Contains information about the file. Each file has 5 data records. We have uploaded the field name

Is 'uploaded _ file', so the following data exists:

Variable $ _ FILES ["uploaded_file"] ["name"] original name of the file uploaded from the user's machine
MIME type of the uploaded file with the variable $ _ FILES ["uploaded_file"] ["type"] (if the browser

Type provided)
The size of the file uploaded in bytes with the variable $ _ FILES ["uploaded_file"] ["size "]
Variable $ _ FILES ["uploaded_file"] ["tmp_name"], which is temporarily stored on the server

Location
Variable $ _ FILES ["uploaded_file"] ["error"] error code uploads results from FILES
The following example accepts an uploaded file and stores it in the upload directory. It allows uploading based on KB only JPEG

Image. The code itself is quite clear, but we will make some explanations. There is an example in appearance and warranty

This is the upload. php code.
// Define heck that we have a file
If ((! Empty ($ _ FILES ["uploaded_file"]) & ($ _ FILES ['uploaded _ file'] ['error']

= 0 )){
// Check if the file is JPEG image and it's size is less than 350Kb
$ Filename = basename ($ _ FILES ['uploaded _ file'] ['name']);
$ Ext = substr ($ filename, strrpos ($ filename, '.') + 1 );
If ($ ext = "jpg") & ($ _ FILES ["uploaded_file"] ["type"] = "image/jpeg ")

&&
($ _ FILES ["uploaded_file"] ["size"] <350000 )){
// Determine the path to which we want to save this file
$ Newname = dirname (_ FILE _). '/upload/'. $ filename;
// Check if the file with the same name is already exists on

Server
If (! File_exists ($ newname )){
// Attempt to move the uploaded file to it's new place
If (move_uploaded_file ($ _ FILES ['uploaded _ file']

['Tmp _ name'], $ newname ))){
Echo "It's done! The file has been saved as: ". $ newname;
} Else {
Echo "Error: A problem occurred during file upload! ";
}
} Else {
Echo "Error: File". $ _ FILES ["uploaded_file"] ["name"]. "already

Exists ";
}
} Else {
Echo "Error: Only. jpg images under 350Kb are accepted for upload ";
}
} Else {
Echo "Error: No file uploaded ";
}
?>
Upload the file you need before this to determine whether the file actually uploads anything. Then we will check

The size of a JPEG image smaller than KB. Next, we will determine the path.

Save the file and check whether the name of the file on the server already exists. When all checks pass,

Copy the file to a permanent location using the move_upload_file () function. This function also confirms

The file you want to process is a legal file Upload result from the user. If the file is successfully uploaded

.

Note: Make sure that PHP allows you to read and write files to the directory where you want to copy files.

This example is actually very simple. it is proposed to demonstrate how to use PHP to upload files. For example, you can add

New condition and allow upload of GIF and PNG images, or any file, you need other types. In this tutorial

Being unfamiliar with PHP may be a good start point.


You can use PHP to manage your uploads. First, you need to build a file to be uploaded as an HTML form on the user interface. There is a look and save in the example below...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.