Home Backend Development PHP Tutorial Specific usage of PHP upload class upload.php_PHP tutorial

Specific usage of PHP upload class upload.php_PHP tutorial

Jul 15, 2016 pm 01:29 PM
php upload for code Instructions about specific bring us yes of kind

What we bring to you today is the specific code for as follows:

<ol class="dp-xml">
<li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN> </SPAN><LI class=alt><SPAN>/**  </SPAN><LI class=""><SPAN> * 我的文件上传类  </SPAN><LI class=alt><SPAN> *  </SPAN><LI class=""><SPAN> * 未完成的功能:  </SPAN><LI class=alt><SPAN> * 1.对目标目录是否存在的判断  </SPAN><LI class=""><SPAN> * 2.如果上传时出现重名,自动重命名  </SPAN><LI class=alt><SPAN> *   </SPAN><LI class=""><SPAN> * @author M.Q. </SPAN><SPAN class=tag><STRONG><FONT color=#006699><</FONT></STRONG></SPAN><SPAN>[url]www.mengqi.net[/url]</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span> </span>
</li>
<li class="alt"><span> */  </span></li>
<li class=""><span>class upload  </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>    /**  </span></li>
<li class="alt"><span>     * PHP上传类upload.php上传文件的信息,此值由构造函数取得,如果上传文件失败或出错或未上传,则此值为false  </span></li>
<li class=""><span>     *  </span></li>
<li class="alt"><span>     * @var array  </span></li>
<li class=""><span>     */  </span></li>
<li class="alt">
<span>    private $</span><span class="attribute"><font color="#ff0000">file</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">false</font></span><span>;   </span>
</li>
<li class=""><span>      </span></li>
<li class="alt"><span>     </span></li>
<li class=""><span>    /**  </span></li>
<li class="alt"><span>     * 构造函数:取得上传文件的信息  </span></li>
<li class=""><span>     *   </span></li>
<li class="alt"><span>     * 如果在上传文件的工程中发生错误,那么出错的文件不会放在结果中返回,结果中的文件都是可用的  </span></li>
<li class=""><span>     *  </span></li>
<li class="alt">
<span>     * @param string $tag form表单中</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN><SPAN class=tag>></span></font></strong><span>标签中name属性的值,例</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"p"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"file"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span> </span>
</li>
<li class=""><span>     *   </span></li>
<li class="alt"><span>     * 例1,上传单个文件:  </span></li>
<li class="">
<span>     * </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"upfile"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"file"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span> </span>
</li>
<li class="alt"><span>     *   </span></li>
<li class=""><span>     * 例2,上传多个文件:  </span></li>
<li class="alt">
<span>     * </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"upfile[]"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"file"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span> </span>
</li>
<li class="">
<span>     * </span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>input</SPAN></FONT></STRONG><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>name</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"upfile[]"</FONT></SPAN><SPAN> </SPAN><SPAN class=attribute><FONT color=#ff0000>type</FONT></SPAN><SPAN>=</SPAN><SPAN class=attribute-value><FONT color=#0000ff>"file"</FONT></SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong><span> </span>
</li>
<li class="alt"><span>     *   </span></li>
<li class=""><span>     * 结果(保存在$file变量中)如下:  </span></li>
<li class="alt"><span>     *   </span></li>
<li class=""><span>     * array(  </span></li>
<li class="alt">
<span>     * [0] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> array(  </span>
</li>
<li class="">
<span>     *      'name'      =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'abc.txt'  </span>
</li>
<li class="alt">
<span>     *      'type'      =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'text/plain’  </span>
</li>
<li class="">
<span>     *      'tmp_name'  =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> '/tmp/phpgxecCb'  </span>
</li>
<li class="alt">
<span>     *      'error'     =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 0  </span>
</li>
<li class="">
<span>     *      'size'      =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 62  </span>
</li>
<li class="alt"><span>     *      )  </span></li>
<li class="">
<span>     * [1] =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> array(  </span>
</li>
<li class="alt">
<span>     *      'name'      =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'abc.txt'  </span>
</li>
<li class="">
<span>     *      'type'      =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 'text/plain’  </span>
</li>
<li class="alt">
<span>     *      'tmp_name'  =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> '/tmp/phpgxecCb'  </span>
</li>
<li class="">
<span>     *      'error'     =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 0  </span>
</li>
<li class="alt">
<span>     *      'size'      =</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 62  </span>
</li>
<li class=""><span>     *      )  </span></li>
<li class="alt"><span>     * )  </span></li>
<li class=""><span>     */  </span></li>
<li class="alt"><span>    public function __construct($tag)  </span></li>
<li class=""><span>    {  </span></li>
<li class="alt">
<span>        $</span><span class="attribute"><font color="#ff0000">file</font></span><span> = $_FILES[$tag];  </span>
</li>
<li class=""><span>                 </span></li>
<li class="alt"><span>        if (!isset($file) || empty($file))   </span></li>
<li class=""><span>        {  </span></li>
<li class="alt"><span>            return; //没有上传文件  </span></li>
<li class=""><span>        }  </span></li>
<li class="alt"><span>          </span></li>
<li class="">
<span>        $</span><span class="attribute"><font color="#ff0000">num</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">count</font></span><span>($file['name']); //PHP上传类upload.php上传文件的个数  </span>
</li>
<li class="alt"><span>          </span></li>
<li class="">
<span>        $</span><span class="attribute"><font color="#ff0000">data</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">array</font></span><span>(); //用来保存上传文件的信息的数组  </span>
</li>
<li class="alt"><span> </span></li>
<li class=""><span>        //上传了多个文件  </span></li>
<li class="alt">
<span>        if ($num </span><span class="tag"><strong><font color="#006699">></font></strong></span><span> 1)  </span>
</li>
<li class=""><span>        {  </span></li>
<li class="alt">
<span>            for($</span><span class="attribute"><font color="#ff0000">i</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; $i </span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN> $num; $i++)  </SPAN></SPAN><LI class=""><SPAN>            {  </SPAN><LI class=alt><SPAN>                $</SPAN><SPAN class=attribute><FONT color=#ff0000>d</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>();  </SPAN></SPAN><LI class=""><SPAN>                $d['name']       = $file['name'][$i];  </SPAN><LI class=alt><SPAN>                $d['type']       = $file['type'][$i];  </SPAN><LI class=""><SPAN>                $d['tmp_name']   = $file['tmp_name'][$i];  </SPAN><LI class=alt><SPAN>                $d['error']      = $file['error'][$i];  </SPAN><LI class=""><SPAN>                $d['size']       = $file['size'][$i];  </SPAN><LI class=alt><SPAN>                  </SPAN><LI class=""><SPAN>                if ($d['error'] == 0)  </SPAN><LI class=alt><SPAN>                {  </SPAN><LI class=""><SPAN>                    $data[] = $d;  </SPAN><LI class=alt><SPAN>                }  </SPAN><LI class=""><SPAN>                else   </SPAN><LI class=alt><SPAN>                {  </SPAN><LI class=""><SPAN>                    @unlink($d['tmp_name']);  </SPAN><LI class=alt><SPAN>                }  </SPAN><LI class=""><SPAN>            }  </SPAN><LI class=alt><SPAN>        }  </SPAN><LI class=""><SPAN>        //只上传了一个文件  </SPAN><LI class=alt><SPAN>        else   </SPAN><LI class=""><SPAN>        {  </SPAN><LI class=alt><SPAN>            $</SPAN><SPAN class=attribute><FONT color=#ff0000>d</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>array</FONT></SPAN><SPAN>();  </SPAN></SPAN><LI class=""><SPAN>            $d['name']       = $file['name'];  </SPAN><LI class=alt><SPAN>            $d['type']       = $file['type'];  </SPAN><LI class=""><SPAN>            $d['tmp_name']   = $file['tmp_name'];  </SPAN><LI class=alt><SPAN>            $d['error']      = $file['error'];  </SPAN><LI class=""><SPAN>            $d['size']       = $file['size'];  </SPAN><LI class=alt><SPAN>              </SPAN><LI class=""><SPAN>            if ($d['error'] == 0)  </SPAN><LI class=alt><SPAN>            {  </SPAN><LI class=""><SPAN>                $data[] = $d;  </SPAN><LI class=alt><SPAN>            }  </SPAN><LI class=""><SPAN>            else   </SPAN><LI class=alt><SPAN>            {  </SPAN><LI class=""><SPAN>                @unlink($d['tmp_name']);  </SPAN><LI class=alt><SPAN>            }  </SPAN><LI class=""><SPAN>        }  </SPAN><LI class=alt><SPAN>          </SPAN><LI class=""><SPAN>        if (empty($data)) return;  </SPAN><LI class=alt><SPAN>          </SPAN><LI class=""><SPAN>        $this -</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> </span><span class="attribute"><font color="#ff0000">file</font></span><span> = $data; //保存上传文件的信息  </span>
</li>
<li class="alt"><span>    }  </span></li>
<li class=""><span>      </span></li>
<li class="alt"><span>    /**  </span></li>
<li class=""><span>     * 将上传的文件从临时文件夹移动到目标路径  </span></li>
<li class="alt"><span>     *  </span></li>
<li class=""><span>     * @param array $src 文件信息数组,是$file数组的其中一个元素(仍然是数组)  </span></li>
<li class="alt"><span>     * @param string $destpath 上传的目标路径  </span></li>
<li class=""><span>     * @param string $filename 上传后的文件名,如果为空,则使用上传时的文件名  </span></li>
<li class="alt"><span>     * @return bool  </span></li>
<li class=""><span>     */  </span></li>
<li class="alt">
<span>    public function save($src, $destpath, $</span><span class="attribute"><font color="#ff0000">filename</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">null</font></span><span>)  </span>
</li>
<li class=""><span>    {  </span></li>
<li class="alt">
<span>        $</span><span class="attribute"><font color="#ff0000">srcTName</font></span><span> = $src['tmp_name']; //原始上传文件的临时文件名  </span>
</li>
<li class="">
<span>        $</span><span class="attribute"><font color="#ff0000">srcFName</font></span><span> = $src['name'];     //原始文件名  </span>
</li>
<li class="alt"><span>          </span></li>
<li class=""><span>        //如果$filename参数为空,则使用上传时的文件名  </span></li>
<li class="alt"><span>        if (empty($filename))  </span></li>
<li class=""><span>        {  </span></li>
<li class="alt">
<span>            $</span><span class="attribute"><font color="#ff0000">filename</font></span><span> = $srcFName;  </span>
</li>
<li class=""><span>        }  </span></li>
<li class="alt"><span>          </span></li>
<li class=""><span>        //$dest是文件最终要复制到的路径和文件名  </span></li>
<li class="alt"><span>        if (empty($destpath))  </span></li>
<li class=""><span>        {  </span></li>
<li class="alt">
<span>            $</span><span class="attribute"><font color="#ff0000">dest</font></span><span> = $filename;  </span>
</li>
<li class=""><span>        }  </span></li>
<li class="alt"><span>        else   </span></li>
<li class=""><span>        {  </span></li>
<li class="alt"><span>            //修正路径中的斜杠,将末尾的修改为/,如果末尾不是也不是/,则给末尾添加一个/  </span></li>
<li class="">
<span>            $</span><span class="attribute"><font color="#ff0000">pathend</font></span><span> = $destpath[strlen($destpath) - 1]; //上传的目标路径的最后一个字符  </span>
</li>
<li class="alt">
<span>            if ($</span><span class="attribute"><font color="#ff0000">pathend</font></span><span> == '\')  </span>
</li>
<li class=""><span>            {  </span></li>
<li class="alt">
<span>                $</span><span class="attribute"><font color="#ff0000">dest</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">substr_replace</font></span><span>($destpath, '/', strlen($destpath)-1).$filename;  </span>
</li>
<li class=""><span>            }  </span></li>
<li class="alt"><span>            else if ($pathend != '/')  </span></li>
<li class=""><span>            {  </span></li>
<li class="alt">
<span>                $</span><span class="attribute"><font color="#ff0000">dest</font></span><span> = $destpath.'/'.$filename;  </span>
</li>
<li class=""><span>            }  </span></li>
<li class="alt"><span>            else   </span></li>
<li class=""><span>            {  </span></li>
<li class="alt">
<span>                $</span><span class="attribute"><font color="#ff0000">dest</font></span><span> = $destpath.$filename;  </span>
</li>
<li class=""><span>            }  </span></li>
<li class="alt"><span>        }  </span></li>
<li class=""><span>            </span></li>
<li class="alt"><span>        //上传文件成功  </span></li>
<li class=""><span>        if (@move_uploaded_file($srcTName, $dest))  </span></li>
<li class="alt"><span>        {  </span></li>
<li class=""><span>              </span></li>
<li class="alt"><span>            return true;   </span></li>
<li class=""><span>        }  </span></li>
<li class="alt"><span>        else   </span></li>
<li class=""><span>        {  </span></li>
<li class="alt"><span>            return false;  </span></li>
<li class=""><span>        }  </span></li>
<li class="alt"><span>    }  </span></li>
<li class=""><span>      </span></li>
<li class="alt"><span>    /**  </span></li>
<li class=""><span>     * 取得上传文件的信息  </span></li>
<li class="alt"><span>     *  </span></li>
<li class=""><span>     * @return array  </span></li>
<li class="alt"><span>     */  </span></li>
<li class=""><span>    public function getFileInfo()  </span></li>
<li class="alt"><span>    {  </span></li>
<li class="">
<span>        return $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>file;  </span>
</li>
<li class="alt"><span>    }  </span></li>
<li class=""><span>}  </span></li>
<li class="alt"><span> </span></li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">a</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> upload('upfile');  </span>
</li>
<li class=""><span> </span></li>
<li class="alt">
<span>$</span><span class="attribute"><font color="#ff0000">fileinfo</font></span><span> = $a -</span><span class="tag"><strong><font color="#006699">></font></strong></span><span> getFileInfo();  </span>
</li>
<li class="">
<span>if ($</span><span class="attribute"><font color="#ff0000">fileinfo</font></span><span> == false)  </span>
</li>
<li class="alt"><span>{  </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>for($</span><span class="attribute"><font color="#ff0000">i</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">0</font></span><span>; $i </span><span class="tag"><strong><font color="#006699"><</FONT></STRONG></SPAN><SPAN> </SPAN><SPAN class=tag-name><STRONG><FONT color=#006699>count</FONT></STRONG></SPAN><SPAN>($fileinfo); $i++)  </SPAN></SPAN><LI class=alt><SPAN>{  </SPAN><LI class=""><SPAN>    echo '正在上传 '.$fileinfo[$i]['name'].' ';  </SPAN><LI class=alt><SPAN>    if ($a -</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></font></strong></span><span> save($fileinfo[$i], 'upload')) echo '完毕';  </span>
</li>
<li class=""><span>    else echo '失败';  </span></li>
<li class="alt">
<span>    echo '</span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>br</SPAN><SPAN class=tag>></span></font></strong><span>';  </span>
</li>
<li class=""><span>}  </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>
Copy after login

The above code is specific about the PHP upload class upload.php How to use.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446389.htmlTechArticleWhat we bring to you today is the specific code as follows: ? php /** *My file upload class * *Unfinished functions: *1. Determine whether the target directory exists *2. If it appears during upload...
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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

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)

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

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

To work on file upload we are going to use the form helper. Here, is an example for file upload.

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

In this chapter, we are going to learn the following topics related to routing ?

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

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

See all articles