首页 > 后端开发 > php教程 > php上传功能集后缀名判断跟随机命名

php上传功能集后缀名判断跟随机命名

WBOY
发布: 2016-06-13 12:24:22
原创
717 人浏览过

php上传功能集后缀名判断和随机命名

form.php

<span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">http-equiv</span><span style="color: #0000ff;">="content-type"</span><span style="color: #ff0000;"> content</span><span style="color: #0000ff;">="text/html"</span><span style="color: #ff0000;"> charset</span><span style="color: #0000ff;">="utf-8"</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>Upload Image<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">method</span><span style="color: #0000ff;">="post"</span><span style="color: #ff0000;"> action</span><span style="color: #0000ff;">="upload.php"</span><span style="color: #ff0000;"> enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="hidden"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="MAX_FILE_SEZE"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="2000000"</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="view"</span><span style="color: #0000ff;">></span>    <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="upload"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="B1"</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span>
登录后复制

upload.php

<span style="color: #000000;">php</span><span style="color: #0000ff;">include</span>("check.php"); <span style="color: #008000;">//</span><span style="color: #008000;"> 引入自定义函数文件</span><span style="color: #800080;">$type</span> = <span style="color: #0000ff;">array</span>("jpg", "gif", "bmp", "jpeg", "png"<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 判断上传文件类型</span><span style="color: #800080;">$fileext</span> = <span style="color: #008080;">strtolower</span>(fileext(<span style="color: #800080;">$_FILES</span>['file']['name'<span style="color: #000000;">]));</span><span style="color: #800080;">$uploadfilename</span> = random(8<span style="color: #000000;">);</span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">in_array</span>(<span style="color: #800080;">$fileext</span>, <span style="color: #800080;">$type</span><span style="color: #000000;">)){    </span><span style="color: #800080;">$filename</span> = <span style="color: #008080;">explode</span>(".", <span style="color: #800080;">$_FILES</span>['file']['name'<span style="color: #000000;">]);        </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>['file']['tmp_name'<span style="color: #000000;">])){</span><span style="color: #008000;">//</span><span style="color: #008000;">        echo $_FILES['file']['tmp_name'];</span>        <span style="color: #800080;">$flag</span> = <span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>['file']['tmp_name'], "/Library/WebServer/Documents/test/".<span style="color: #800080;">$uploadfilename</span>.".".<span style="color: #800080;">$fileext</span><span style="color: #000000;">);        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$flag</span><span style="color: #000000;">){            </span><span style="color: #0000ff;">echo</span> "上传成功!"<span style="color: #000000;">;        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{            </span><span style="color: #0000ff;">echo</span> "Error."<span style="color: #000000;">;        }        </span><span style="color: #0000ff;">echo</span> "<a href="javascript:history.go(-1)">Back</a>"<span style="color: #000000;">;    }}</span>
登录后复制

check.php

<span style="color: #000000;">php</span><span style="color: #008080;">header</span>("Content-type:text/html;charset=utf8"<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取文件后缀名函数</span><span style="color: #0000ff;">function</span> fileext(<span style="color: #800080;">$filename</span><span style="color: #000000;">){    </span><span style="color: #800080;">$sTemp</span> = <span style="color: #008080;">strrchr</span>(<span style="color: #800080;">$filename</span>, "."<span style="color: #000000;">);    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">substr</span>(<span style="color: #800080;">$sTemp</span>, 1<span style="color: #000000;">);}</span><span style="color: #0000ff;">function</span> fileext2(<span style="color: #800080;">$filename</span><span style="color: #000000;">){    </span><span style="color: #800080;">$sTemp</span> = <span style="color: #008080;">explode</span>(".", <span style="color: #800080;">$filename</span><span style="color: #000000;">);    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$sTemp</span>[<span style="color: #008080;">count</span>(<span style="color: #800080;">$sTemp</span>)-1<span style="color: #000000;">];}</span><span style="color: #008000;">//</span><span style="color: #008000;"> 生成随机文件名函数</span><span style="color: #0000ff;">function</span> random(<span style="color: #800080;">$length</span><span style="color: #000000;">){    </span><span style="color: #800080;">$captchaSource</span> = "0123456789abcdefghijklmnopqrstuvwxyz这是一个随机打印输出字符串的例子"<span style="color: #000000;">;    </span><span style="color: #800080;">$captchaResult</span> = "2015"; <span style="color: #008000;">//</span><span style="color: #008000;"> 随机数返回值</span>    <span style="color: #800080;">$captchaSentry</span> = ""; <span style="color: #008000;">//</span><span style="color: #008000;"> 随机数中间变量</span>    <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$length;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){        </span><span style="color: #800080;">$n</span> = <span style="color: #008080;">rand</span>(0, 35); <span style="color: #008000;">#</span><span style="color: #008000;">strlen($captchaSource));</span>        <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$n</span> >= 36<span style="color: #000000;">){            </span><span style="color: #800080;">$n</span> = 36 + <span style="color: #008080;">ceil</span>((<span style="color: #800080;">$n</span>-36)/3) * 3<span style="color: #000000;">;            </span><span style="color: #800080;">$captchaResult</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$captchaSource</span>, <span style="color: #800080;">$n</span>, 3<span style="color: #000000;">);        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{            </span><span style="color: #800080;">$captchaResult</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$captchaSource</span>, <span style="color: #800080;">$n</span>, 1<span style="color: #000000;">);        }    }    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$captchaResult</span><span style="color: #000000;">;}</span>?>
登录后复制

将三个文件整合成一个:

<span style="color: #000000;">php</span><span style="color: #008000;">//</span><span style="color: #008000;"> 获取文件后缀名函数</span><span style="color: #0000ff;">function</span> fileext(<span style="color: #800080;">$filename</span><span style="color: #000000;">){    </span><span style="color: #800080;">$sTemp</span> = <span style="color: #008080;">strrchr</span>(<span style="color: #800080;">$filename</span>, "."<span style="color: #000000;">);    </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">substr</span>(<span style="color: #800080;">$sTemp</span>, 1<span style="color: #000000;">);}</span><span style="color: #0000ff;">function</span> fileext2(<span style="color: #800080;">$filename</span><span style="color: #000000;">){    </span><span style="color: #800080;">$sTemp</span> = <span style="color: #008080;">explode</span>(".", <span style="color: #800080;">$filename</span><span style="color: #000000;">);    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$sTemp</span>[<span style="color: #008080;">count</span>(<span style="color: #800080;">$sTemp</span>)-1<span style="color: #000000;">];}</span><span style="color: #008000;">//</span><span style="color: #008000;"> 生成随机文件名函数</span><span style="color: #0000ff;">function</span> random(<span style="color: #800080;">$length</span><span style="color: #000000;">){    </span><span style="color: #800080;">$captchaSource</span> = "0123456789abcdefghijklmnopqrstuvwxyz这是一个随机打印输出字符串的例子"<span style="color: #000000;">;    </span><span style="color: #800080;">$captchaResult</span> = "2015"; <span style="color: #008000;">//</span><span style="color: #008000;"> 随机数返回值</span>    <span style="color: #800080;">$captchaSentry</span> = ""; <span style="color: #008000;">//</span><span style="color: #008000;"> 随机数中间变量</span>    <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$length;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){        </span><span style="color: #800080;">$n</span> = <span style="color: #008080;">rand</span>(0, 35); <span style="color: #008000;">#</span><span style="color: #008000;">strlen($captchaSource));</span>        <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$n</span> >= 36<span style="color: #000000;">){            </span><span style="color: #800080;">$n</span> = 36 + <span style="color: #008080;">ceil</span>((<span style="color: #800080;">$n</span>-36)/3) * 3<span style="color: #000000;">;            </span><span style="color: #800080;">$captchaResult</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$captchaSource</span>, <span style="color: #800080;">$n</span>, 3<span style="color: #000000;">);        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{            </span><span style="color: #800080;">$captchaResult</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$captchaSource</span>, <span style="color: #800080;">$n</span>, 1<span style="color: #000000;">);        }    }    </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$captchaResult</span><span style="color: #000000;">;}</span><span style="color: #800080;">$type</span> = <span style="color: #0000ff;">array</span>("jpg", "gif", "bmp", "jpeg", "png"<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 判断上传文件类型</span><span style="color: #800080;">$fileext</span> = <span style="color: #008080;">strtolower</span>(fileext(<span style="color: #800080;">$_FILES</span>['file']['name'<span style="color: #000000;">]));</span><span style="color: #800080;">$uploadfilename</span> = random(8<span style="color: #000000;">);</span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">in_array</span>(<span style="color: #800080;">$fileext</span>, <span style="color: #800080;">$type</span><span style="color: #000000;">)){    </span><span style="color: #800080;">$filename</span> = <span style="color: #008080;">explode</span>(".", <span style="color: #800080;">$_FILES</span>['file']['name'<span style="color: #000000;">]);    </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>['file']['tmp_name'<span style="color: #000000;">])){</span><span style="color: #008000;">//</span><span style="color: #008000;">        echo $_FILES['file']['tmp_name'];</span>        <span style="color: #800080;">$flag</span> = <span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>['file']['tmp_name'], "/Library/WebServer/Documents/test/".<span style="color: #800080;">$uploadfilename</span>.".".<span style="color: #800080;">$fileext</span><span style="color: #000000;">);        </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$flag</span><span style="color: #000000;">){            </span><span style="color: #0000ff;">echo</span> "上传成功!"<span style="color: #000000;">;        }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{            </span><span style="color: #0000ff;">echo</span> "Error."<span style="color: #000000;">;        }        </span><span style="color: #0000ff;">echo</span> "<a href="javascript:history.go(-1)">Back</a>"<span style="color: #000000;">;    }}</span>?>    <meta http-equiv="content-type" content="text/html" charset="utf-8">    <title>Upload Image</title>
登录后复制

 

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板