Home >Backend Development >PHP Tutorial >php上传功能集后缀名判断跟随机命名

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

WBOY
WBOYOriginal
2016-06-13 12:24:22719browse

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>

 

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