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

Mining PHP upload file type principle implementation_PHP tutorial

Jul 15, 2016 pm 01:27 PM
php upload Function principle accomplish bring powerful us dig document have Know type

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>
Copy after login

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) &amp;&amp;&amp;&amp; !$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>
Copy after login

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 of this Website
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

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles