ホームページ  >  記事  >  バックエンド開発  >  PHPアップロードクラスの具体的な使い方upload.php_PHPチュートリアル

PHPアップロードクラスの具体的な使い方upload.php_PHPチュートリアル

WBOY
WBOYオリジナル
2016-07-15 13:29:071346ブラウズ

今日紹介するのは具体的なコードは次のとおりです:

<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>

上記のコードは、 PHP アップロード クラスのアップロード.php 固有の手順。


www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/446389.html技術記事今回紹介するのは以下のような具体的なコードです: ? php /** *私のファイルアップロードクラス* *未完成の機能: *1. アップロード中に表示された場合は... .
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。