PHP---File upload and download, _php tutorial

Source: Internet
Author: User

PHP---File upload and download,


Transfer from http://www.cnblogs.com/lazycat-cz/p/4113037.html

---Level of safety can not be enough ╮ (╯_╰) ╭

File Upload---> is to upload local files to the server. (HTTP protocol to learn) first to select the uploaded files locally, after uploading to the server, the service side to do some processing, for this client and the service side to do some settings

The most basic way to upload (a client) file is to post the file via the form form, putting the code first.

  

The Enctype property of a label specifies what type of content to use when submitting a form. Use "Multipart/form-data" when the form requires binary data, such as the contents of a file.

The type= "file" attribute of the tag specifies that the input should be treated as a file. For example, when previewing in a browser, you'll see a browse button next to the input box.

(server side) files are uploaded to the servers and undergo some process. In PHP, $_post saves post-delivered data, and information about the uploaded file is stored in $_files,

  PhpEcho' _files:
';//
A common application of tags is to represent the source code of the computer.    Print_r($_files); Echo' _post:
';     Print_r ($_post);? >

_files[] is a two-dimensional array. Array[uploadfile] Key name depends onType= the name value in the "File" tab. It marks the upload file information of this control, so we can put more than one upload control, set a different name, of course, the same name can be set, can completely put them all in an array inside, such as。 Error, there are several cases, 0: no error, upload success, 1: The file exceeds the PHP configuration directive upload_max_filesize the size specified, 2: The file exceeds the size specified in the HTML form max_file_size, 3: File only partially uploaded; 4: no file upload. (size problem is still not clear ╮ (╯_╰) ╭, for the moment do not explain)

      Php$typeWhiteList=Array(' txt ', ' Doc ', ' php ', ' zip ', ' exe ');//type Whitelist, filter file types that are not allowed to be uploaded    $max _size= 1000000;//size limit is 1M    $upload _path= ' D:/wamp ';//Specify the directory to be moved to//1 to determine if the server was successfully uploaded    $error=$_files[' UploadFile '] [' Error ']; if($error> 0){         Switch($error){              Case1:Exit(' Maximum file upload limit over PHP configuration ');  Case2:Exit(' Maximum file upload limit over HTML form ');  Case3:Exit(' files are only partially uploaded ');  Case4:Exit(' No files uploaded '); default:Exit(' Unknown type error '); }    }         //2, determine whether to allow the type of upload    $extension=PathInfo($_files[' UploadFile '] [' name '], pathinfo_extension);//Get extension    if(!In_array($extension,$typeWhiteList)){        if($extension== '')           Exit(' not allowed to upload empty type files '); Else            Exit(' Do not allow uploads '.$extension.' Type file '); }          //3, determine whether the allowable size    if($_files[' UploadFile '] [' Size '] >$max _size){        Exit(' exceeding the allowable upload '.$max _size.' bytes); }         //4, has reached the specified location    $filename=Date(' YMD ').Rand(1000, 9999);//generate a new file name to prevent overwriting    if(Is_uploaded_file($_files[' UploadFile '] [' Tmp_name '])) {//decide whether to upload via HTTP post        if(!Move_uploaded_file($_files[' UploadFile '] [' Tmp_name '],$upload _path.$filename.'.'.$extension)){            Exit(' cannot move to specified position '); }         Else{            Echo' File Upload succeeded
'; Echo' File name: '.$upload _path.$filename.'.'.$extension.'
'; } } Else{ Exit(' File not uploaded by legal means '); }

Upload complete ..... .....

File Download---> Single file download only need to use an HTML link is enough, using the tag, href attribute to specify the resource location, a little. However, this approach can only handle MIME types that are not recognized by the browser by default (mime specific wiki Wikipedia http://zh.wikipedia.org/wiki/%E5%A4%9A%E7%94%A8%E9%80%94%E4%BA%92%E8% 81%AF%E7%B6%B2%E9%83%B5%E4%BB%B6%E6%93%B4%E5%B1%95)

                 Donwload <span>file</span>             
 
                            header. txt
Php.zip
Pic.ico

For these browsers do not know the type of file, click the link, it directly to the frame let you download, some browsers even directly down, then for text txt, JPG and other browsers default recognition of the type of file, a click will be directly displayed on the page, such as the above Header.txt, Pic.ico. How to download them without showing them on the page, use the header function.

The header function will be sent to the header information, please take the file as an attachment, so that when clicked, it will also be downloaded. (not very well understood, fully understood the supplement ╮ (╯_╰) ╭)

Oh ~ The first blog post →_→

Re-declare the reprint address http://www.cnblogs.com/lazycat-cz/p/4113037.html

http://www.bkjia.com/PHPjc/971767.html www.bkjia.com true http://www.bkjia.com/PHPjc/971767.html techarticle PHP---File upload and download, transfer from http://www.cnblogs.com/lazycat-cz/p/4113037.html security---Level is not enough ╮ (╯_╰) ╭ File Upload---is the local file upload to the service ...

  • 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.