Simple PHP file upload (instance) _ php Tutorial-PHP Tutorial

Source: Internet
Author: User
Simple PHP file upload (instance ). Save the following code as uploads. php and copy it as follows :! DOCTYPEhtmlPUBLIC-W3CDTDXHTML1.0TransitionalENwww.w3.orgTRxhtml1DTDxhtml1-transitional.dt save the following code as uploads. php

The code is as follows:







Simple file Upload












Function file_list ($ dir, $ pattern = ""){
$ Arr = array ();
$ Dir_handle = opendir ($ dir );
If ($ dir_handle ){
While ($ file = readdir ($ dir_handle ))! = False ){
If ($ file = '.' | $ file = '..'){
Continue;
}
$ Tmp = realpath ($ dir. '/'. $ file );
If (is_dir ($ tmp )){
$ RetArr = file_list ($ tmp, $ pattern );
If (! Empty ($ retArr )){
$ Arr [] = $ retArr;
}
} Else {
If ($ pattern = "" | preg_match ($ pattern, $ tmp )){
$ Arr [] = $ tmp;
}
}
}
Closedir ($ dir_handle );
}
Return $ arr;
}
$ D_root = $ _ SERVER ['document _ root'];
$ Store_dir = "$ d_root/uploads/"; // storage location of the uploaded file
If (! Is_dir ($ store_dir )){
Mkdir ($ store_dir, 0777, true );
}
$ File_arr = file_list ($ store_dir );
Foreach ($ file_arr as $ v => $ k ){
$ D_root_no = strlen ($ d_root );
$ L = substr ($ k, $ d_root_no );
Echo $ v. ':'. $ _ SERVER ['server _ ADDR ']. $ l .'
';
}
$ Upload_file = isset ($ _ FILES ['upload _ file'] ['tmp _ name'])? $ _ FILES ['upload _ file'] ['tmp _ name']: '';
$ Upload_file_name = isset ($ _ FILES ['upload _ file'] ['name'])? $ _ FILES ['upload _ file'] ['name']: '';
$ Upload_file_size = isset ($ _ FILES ['upload _ file'] ['size'])? $ _ FILES ['upload _ file'] ['size']: '';
If ($ upload_file ){
$ File_size_max = 1000*1000*200; // MB limit the maximum file upload capacity (bytes)
If (! Is_dir ($ store_dir )){
Mkdir ($ store_dir, 0777, true );
}
$ 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;
}
}
If (isset ($ _ FILES ['upload _ file']) {
Echo"

You uploaded the file :";
Echo isset ($ _ FILES ['upload _ file'] ['name'])? $ _ FILES ['upload _ file'] ['name']: '';
Echo"
";
// The original name of the client machine file.

Echo "the MIME type of the file is :";
Echo isset ($ _ FILES ['upload _ file'] ['type'])? $ _ 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 isset ($ _ FILES ['upload _ file'] ['size'])? $ _ FILES ['upload _ file'] ['size']: '';
// Size of the uploaded file, in bytes.
Echo"
";

Echo "the file is temporarily stored after being uploaded :";
Echo isset ($ _ FILES ['upload _ file'] ['tmp _ name'])? $ _ FILES ['upload _ file'] ['tmp _ name']: '';
// Temporary file name stored on the server after the file is uploaded.
$ Erroe = isset ($ _ FILES ['upload _ file'] ['error'])? $ _ 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;
Case 6:
Echo "no cache directory"; break;
Case 7:
Echo "the upload directory cannot be read"; break;
Case 8:
Echo "Upload stopped"; break;
Default:
Echo "file upload not selected"; break;
}
Echo"

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.