Home  >  Article  >  Backend Development  >  PHP---File upload and download

PHP---File upload and download

WBOY
WBOYOriginal
2016-08-08 09:27:47768browse

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

Safety performance---insufficient level ╮(╯_╰)╭

File upload--->It is to upload local files to the server. (HTTP protocol needs to be learned)First, you need to select the uploaded file locally. After uploading to the server, the server needs to do some processing. For this, both the client and the server need to make some settings

(Client) The most basic method of file upload is to POST the file through the form and paste the code first.

<html>
<body>

<form action="upload_file.php" method="post"  enctype="multipart/form-data">
<label <span>for</span>="file">选择文件:</label>
<input type="file" name="uploadFile" /> <br /><br><input type="submit" name="submit" value="上传" /> </form> </body> </html>

The enctype attribute of the tag specifies which content type to use when submitting the form. Use "multipart/form-data" when your form requires binary data, such as file content.

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

(Server) After the file is uploaded to the server, it still needs to go through some processing. In php, $_POST saves the data passed by post, and the relevant information of the uploaded file is saved in $_FILES,

<?<span>php
    </span><span>echo</span> '_FILES: <pre class="brush:php;toolbar:false">'<span>;
</span><span>//</span><span><pre class="brush:php;toolbar:false"> 标签的一个常见应用就是用来表示计算机的源代码。</span>
    <span>print_r</span>(<span>$_FILES</span><span>);
      
    </span><span>echo</span> '_POST: <pre class="brush:php;toolbar:false">'<span>;
    </span><span>print_r</span>(<span>$_POST</span><span>);
</span>?>

_FILES[] is a two-dimensional array. The array[uploadFile] key name depends on the name value in the type="file" tag. It marks the uploaded file information of this control, so we can put multiple upload controls and set different names. Of course, we can also set the same name. We can put them all in an array, such as . error means error, and there are several situations: 0: No error, upload is successful; 1: The file exceeds the size specified by upload_max_filesize in the PHP configuration directive; 2: The file exceeds the size specified by MAX_FILE_SIZE in the HTML form, 3: The file is only partial Upload; 4: No files uploaded. (The size issue is still not clear ╮(╯_╰)╭, so I won’t explain it for now)

<?<span>php
    </span><span>$typeWhiteList</span> = <span>array</span>('txt', 'doc', 'php', 'zip', 'exe');   <span>//</span><span> 类型白名单,过滤不允许上传的文件类型</span>
    <span>$max_size</span> = 1000000;  <span>//</span><span> 大小限制 为1M</span>
    <span>$upload_path</span> = 'D:/WAMP';    <span>//</span><span> 指定移至的目录
     
    // 1、判断是否成功上传到服务器 </span>
    <span>$error</span> = <span>$_FILES</span>['uploadFile']['error'<span>];
    </span><span>if</span>(<span>$error</span> > 0<span>){
         </span><span>switch</span>(<span>$error</span><span>){
             </span><span>case</span> 1: <span>exit</span>('超过php配置的最大文件上传限制'<span>);
             </span><span>case</span> 2: <span>exit</span>('超过HTML表单的最大文件上传限制'<span>);
             </span><span>case</span> 3: <span>exit</span>('文件只有部分被上传'<span>);
             </span><span>case</span> 4: <span>exit</span>('没有上传任何文件'<span>);
             </span><span>default</span>: <span>exit</span>('未知类型错误'<span>);
         }
    }
     
    </span><span>//</span><span> 2、判断是否为允许上传的类型</span>
    <span>$extension</span> = <span>pathinfo</span>(<span>$_FILES</span>['uploadFile']['name'], PATHINFO_EXTENSION); <span>//</span><span> 获取扩展名</span>
    <span>if</span>(!<span>in_array</span>(<span>$extension</span>, <span>$typeWhiteList</span><span>)){
        </span><span>if</span>(<span>$extension</span> == ''<span>)
           </span><span>exit</span>('不允许上传空类型文件'<span>);
         </span><span>else</span> 
           <span>exit</span>('不允许上传'.<span>$extension</span>.'类型文件'<span>);
    } 
     
    </span><span>//</span><span> 3、判断是否为允许大小</span>
    <span>if</span>(<span>$_FILES</span>['uploadFile']['size'] > <span>$max_size</span><span>){
        </span><span>exit</span>('超过了允许上传到的'.<span>$max_size</span>.'字节'<span>);
    }
     
    </span><span>//</span><span> 4、已到指定位置</span>
    <span>$filename</span> = <span>date</span>('Ymd').<span>rand</span>(1000, 9999);   <span>//</span><span> 生成一个新文件名,防止覆盖</span>
    <span>if</span>(<span>is_uploaded_file</span>(<span>$_FILES</span>['uploadFile']['tmp_name'])){   <span>//</span><span> 判断是否通过HTTP POST上传</span>
        <span>if</span>(!<span>move_uploaded_file</span>(<span>$_FILES</span>['uploadFile']['tmp_name'], <span>$upload_path</span>.<span>$filename</span>.'.'.<span>$extension</span><span>)){
            </span><span>exit</span>('无法移动到指定位置'<span>);
         }
         </span><span>else</span><span>{
            </span><span>echo</span> '文件上传成功<br/>'<span>;
            </span><span>echo</span> '文件名: '.<span>$upload_path</span>.<span>$filename</span>.'.'.<span>$extension</span>.'<br>'<span>;
         }
    }
     </span><span>else</span><span>{
         </span><span>exit</span>('文件未通过合法途径上传'<span>);
     }</span>

Upload completed............

File download---> To download a single file, you only need to use an HTML link. Use the tag and the href attribute to specify the resource location. Just click. But this method can only handle MIME types that the browser cannot recognize by default. (MIME details are attached to 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)

<html>
    <head>
             <title>donwload <span>file</span></title>
             <meta http-equiv="Content-Type" content="text/html"; charset="utf-8" />
    </head>
    <body>
             <a href="resource/header.txt"><span>header</span>.txt</a><br/>
             <a href="resource/php.zip">php.zip</a><br/>
             <a href="resource/pic.ico">pic.ico</a>
           
    </body>
</html>

For these types of files that are not recognized by the browser, click on the link, and it will directly pop up a box for you to download. Some browsers even download it directly. So for text, txt, jpg and other types of files that are recognized by browsers by default, just click It will be displayed directly on the page, such as header.txt and pic.ico above. How to download them without displaying them on the page, use the header function.

The header function will notify you by sending header information. Please treat the file as an attachment, so that it will be downloaded when clicked. (I don’t quite understand it yet, I’ll add more after I fully understand it╮(╯_╰)╭)

Oh~

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

The above introduces PHP---file uploading and downloading, including related content. I hope it will be helpful to friends who are interested in PHP tutorials.