Upload a file code instance using php

Source: Internet
Author: User
Tags php file upload
To upload FILES in php, use the ultra-Global Array $ _ FILES. in the form, add the sentence enctype = & quot; multipart/form-data & quot, otherwise, $ _ FILES cannot be set. To upload FILES in php, use the ultra-Global Array $ _ FILES. you must add enctype = "multipart/form-data" to the form, otherwise, $ _ FILES cannot be set.
The $ _ FILES array information is as follows:
$ _ FILES ['upload _ file'] ['name'] // Obtain the file name.
$ _ FILES ['upload _ file'] ['tmp _ name'] // get the temporary storage location
$ _ FILES ['upload _ file'] ['size'] // get the file size
$ _ FILES ['upload _ file'] ['type'] // Obtain the object MIME type.
The following is the instance code:


Php file Upload instance




$ Upload_file = $ _ FILES ['upload _ file'] ['tmp _ name'];
$ Upload_file_name = $ _ FILES ['upload _ file'] ['name'];
If ($ upload_file ){
$ File_size_max = 1000000; // 1 MB limit the maximum file upload capacity (bytes)
$ Store_dir = "E:/"; // storage location of the uploaded file
$ Accept_overwrite = 1; // whether to overwrite the same file
// Check the file size
If ($ upload_file_size> $ file_size_max ){
Echo "Sorry, your file capacity exceeds the limit ";
Exit;
}
// Check the read/write file
If (file_exists ($ store_dir. $ upload_file_name )&&! $ Accept_overwrite ){
Echo "files with the same file name exist ";
Exit;
}
// Copy the file to the specified directory
If (! Move_uploaded_file ($ upload_file, $ store_dir. $ upload_file_name )){
Echo "failed to copy the file ";
Exit;
}
}
Echo"

You uploaded the file :";
Echo $ _ FILES ['upload _ file'] ['name'];
Echo"
";
// The original name of the client machine file.
Echo "the MIME type of the file is :";
Echo $ _ FILES ['upload _ file'] ['type'];
// MIME type of the file, which must be supported by the browser, for example, "image/gif ".
Echo"
";
Echo "Upload file size :";
Echo $ _ FILES ['upload _ file'] ['size'];
// Size of the uploaded file, in bytes.
Echo"
";
Echo "the file is temporarily stored after being uploaded :";
Echo $ _ FILES ['upload _ file'] ['tmp _ name'];
// Temporary file name stored on the server after the file is uploaded.
Echo"
";
$ Erroe = $ _ FILES ['upload _ file'] ['error'];
Switch ($ Erroe ){
Case 0:
Echo "uploaded successfully"; break;
Case 1:
Echo "the uploaded file exceeds the limit of the upload_max_filesize option in php. ini."; break;
Case 2:
Echo "the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. "; Break;
Case 3:
Echo "only part of the file is uploaded"; break;
Case 4:
Echo "no file is uploaded"; break;
}
?>

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.