PHP learning-PHP File Upload

Source: Internet
Author: User
Use an example to record how to upload a file using PHP: 1. Compile index. php on the upload page. The Code is as follows: formactionupload. phpmethodpostenctypemultipartform-upload. Note that the upload file must be specified in the form.

Use an example to record how to upload files using PHP: 1. Compile the index of the upload interface. php, the Code is as follows: form action = upload. php method = post enctype = multipart/form-datainput type = file name = fileinput type = submit value = upload/form

Use an example to record how to upload files using PHP:

1. Compile index. php on the upload page. The Code is as follows:

It must be noted that the enctype attribute must be set to multipart/form-data in the form to be uploaded. 

2. Receive the file and write another file named upload. php. The Code is as follows:

 30*1024) {echo "file size invalid, size> 30kb ";} else {// if an error occurs, the error message if ($ _ FILES ["file"] ["error"]> 0) {echo "upload file error! ";} Else {// No error, copy the uploaded file to the specified directory $ name = $ _ FILES [" file "] [" name "]; $ dir = "upload/"; // if the directory does not exist, create if (! File_exists ($ dir) {mkdir ("upload/");} // checks whether the object already exists if (file_exists ("upload /". $ name) {echo "file exist";} else {// the file does not exist in the specified directory, then copy the file move_uploaded_file ($ _ FILES ["file"] ["tmp_name"], "upload /". $ _ FILES ["file"] ["name"]); echo "upload file complete" ;}}} else {echo "file type invalid" ;}?>
When a file is uploaded, the file information is stored in the $ _ FILES Global Array. The data contains information such as the file name, file type, and file size, which are summarized as follows:

  • $ _ FILES ["file"] ["name"]-name of the uploaded file
  • $ _ FILES ["file"] ["type"]-type of the file to be uploaded
  • $ _ FILES ["file"] ["size"]-size of the uploaded file, in bytes
  • $ _ FILES ["file"] ["tmp_name"]-name of the temporary copy of the file stored on the server
  • $ _ FILES ["file"] ["error"]-error code caused by file Upload

In the above example, a temporary copy of the uploaded file is created in the PHP temporary folder on the server.

The temporary copy file will disappear at the end of the script. To save the uploaded file, we need to copy it to another location. Therefore, in the code above, we created a directory and copied the temporary file to the directory, note that the code of the received file contains the following methods:

① Determine whether a file or directory exists: file_exists ()

② Create a directory: mkdir ()

③ Move the uploaded file from the temporary directory to the specified directory: move_uploaded_file (). This method has two parameters. The first one represents the file path of the temporary directory, the second represents the file path to be moved.

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.