Heim > Backend-Entwicklung > PHP-Tutorial > PHP上传类upload.php的具体使用方法_PHP教程

PHP上传类upload.php的具体使用方法_PHP教程

WBOY
Freigeben: 2016-07-15 13:29:07
Original
1382 Leute haben es durchsucht

我们今天为大家带来的是关于具体代码如下:

<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>
<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> * 1.对目标目录是否存在的判断  </span></li>
<li class=""><span> * 2.如果上传时出现重名,自动重命名  </span></li>
<li class="alt"><span> *   </span></li>
<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">></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 class="tag-name">input</span><span class="tag">></span></span></font></strong><span>标签中name属性的值,例</span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></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">></font></strong></span><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 class="tag-name">input</span></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">></font></strong></span><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 class="tag-name">input</span></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">></font></strong></span><span> </span>
</li>
<li class="">
<span>     * </span><strong><font color="#006699"><span class="tag"><span class="tag-name">input</span></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">></font></strong></span><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>
</li>
<li class=""><span>            {  </span></li>
<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>
</li>
<li class=""><span>                $d['name']       = $file['name'][$i];  </span></li>
<li class="alt"><span>                $d['type']       = $file['type'][$i];  </span></li>
<li class=""><span>                $d['tmp_name']   = $file['tmp_name'][$i];  </span></li>
<li class="alt"><span>                $d['error']      = $file['error'][$i];  </span></li>
<li class=""><span>                $d['size']       = $file['size'][$i];  </span></li>
<li class="alt"><span>                  </span></li>
<li class=""><span>                if ($d['error'] == 0)  </span></li>
<li class="alt"><span>                {  </span></li>
<li class=""><span>                    $data[] = $d;  </span></li>
<li class="alt"><span>                }  </span></li>
<li class=""><span>                else   </span></li>
<li class="alt"><span>                {  </span></li>
<li class=""><span>                    @unlink($d['tmp_name']);  </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>        else   </span></li>
<li class=""><span>        {  </span></li>
<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>
</li>
<li class=""><span>            $d['name']       = $file['name'];  </span></li>
<li class="alt"><span>            $d['type']       = $file['type'];  </span></li>
<li class=""><span>            $d['tmp_name']   = $file['tmp_name'];  </span></li>
<li class="alt"><span>            $d['error']      = $file['error'];  </span></li>
<li class=""><span>            $d['size']       = $file['size'];  </span></li>
<li class="alt"><span>              </span></li>
<li class=""><span>            if ($d['error'] == 0)  </span></li>
<li class="alt"><span>            {  </span></li>
<li class=""><span>                $data[] = $d;  </span></li>
<li class="alt"><span>            }  </span></li>
<li class=""><span>            else   </span></li>
<li class="alt"><span>            {  </span></li>
<li class=""><span>                @unlink($d['tmp_name']);  </span></li>
<li class="alt"><span>            }  </span></li>
<li class=""><span>        }  </span></li>
<li class="alt"><span>          </span></li>
<li class=""><span>        if (empty($data)) return;  </span></li>
<li class="alt"><span>          </span></li>
<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>
</li>
<li class="alt"><span>{  </span></li>
<li class=""><span>    echo '正在上传 '.$fileinfo[$i]['name'].' ';  </span></li>
<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 class="tag-name">br</span><span class="tag">></span></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>
Nach dem Login kopieren

以上代码就是关于PHP上传类upload.php的具体使用方法。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446389.htmlTechArticle我们今天为大家带来的是关于 具体代码如下: ? php /** *我的文件上传类 * *未完成的功能: *1.对目标目录是否存在的判断 *2.如果上传时出现...
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage