Home >Backend Development >PHP Tutorial >Mining PHP upload file type principle implementation_PHP tutorial

Mining PHP upload file type principle implementation_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-15 13:27:02792browse

We all know that PHP has powerful functions and brings me a lot of convenience. Let’s take a look at the implementation of PHP upload file types. PHP supports the function of uploading files, but not all websites that support PHP support this function, especially free websites.

To implement uploading, you must first add the "

" form for uploading files to the HTML.
<ol class="dp-xml">
<li class="alt"><span><span><form </span><span class="attribute"><font color="#ff0000">method</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">post</font></span><span> </span><span class="attribute"><font color="#ff0000">action</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"upload.php"</font></span><span> </span><span class="attribute"><font color="#ff0000">ENCTYPE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"multipart/form-data"</font></span><span>>  </span></span></li>
<li class="">
<span><input </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"file"</font></span><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"upload_file"</font></span><span>>  </span>
</li>
<li class="alt">
<span><input </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"submit"</font></span><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"submit"</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"上传文件"</font></span><span>> </span>
</li>
</ol>

When uploading file types using PHP, you need to check the content in detail, such as whether the file is allowed to be read and written, whether the file format and file size are within the size you specify, etc.

<ol class="dp-xml">
<li class="alt"><span><span><?  </span></span></li>
<li class="">
<span>$</span><span class="attribute">file_size_max</span><span> = </span><span class="attribute-value">1000000</span><span>;  </span>
</li>
<li class="alt"><span>// 限制文件上传最大容量(bytes)  </span></li>
<li class="">
<span>$</span><span class="attribute">store_dir</span><span> = </span><span class="attribute-value">"/public/www/upload/"</span><span>;  </span>
</li>
<li class="alt"><span>// 上传文件的储存位置  </span></li>
<li class="">
<span>$</span><span class="attribute">accept_overwrite</span><span> = </span><span class="attribute-value">true</span><span>;  </span>
</li>
<li class="alt"><span>//允许读写文件  </span></li>
<li class=""><span>// 检查文件大小  </span></li>
<li class="alt"><span>if ($upload_file_size > $file_size_max) {  </span></li>
<li class=""><span>echo "对不起,你的文件容量大于规定";  </span></li>
<li class="alt"><span>exit;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>// 检查读写文件  </span></li>
<li class=""><span>if (file_exists($store_dir . $upload_file_name) &&&& !$accept_overwrite) {  </span></li>
<li class="alt"><span>echo "文件已存在,不能再复制";  </span></li>
<li class=""><span>exit;  </span></li>
<li class="alt"><span>}  </span></li>
<li class=""><span>//复制文件到指定目录  </span></li>
<li class="alt"><span>if (! @ copy($upload_file,$store_dir . $upload_file_name)) {  </span></li>
<li class=""><span>echo "复制文件失败";  </span></li>
<li class="alt"><span>exit;  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span>echo "上传文件完成";  </span></li>
<li class=""><span>?> </span></li>
</ol>

It should be noted that when PHP uploads file types, it copies the file to the server temporary directory (temp), and then uses PHP's "copy( )" function copies files from the temporary directory to the storage directory you specify. Since the program will use a temporary directory to work, if the server blocks the above functions due to security concerns, you will not be able to use PHP's upload function. In addition, the uploaded file directory also needs to set the file mode to 777 (CHMOD 777), otherwise PHP will not have permission to read and write files.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446555.htmlTechArticleWe all know that PHP has powerful functions and brings me a lot of convenience. Let’s take a look at PHP upload here. File type implementation. PHP supports the function of uploading files, but not all support...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn