php上傳功能集後綴名判斷與隨機命名

WBOY
發布: 2016-07-29 09:15:41
原創
719 人瀏覽過

form.php

<span><</span><span>html</span><span>></span><span><</span><span>head</span><span>></span><span><</span><span>meta </span><span>http-equiv</span><span>="content-type"</span><span> content</span><span>="text/html"</span><span> charset</span><span>="utf-8"</span><span>></span><span><</span><span>title</span><span>></span>Upload Image<span></</span><span>title</span><span>></span><span></</span><span>head</span><span>></span><span><</span><span>body</span><span>></span><span><</span><span>form </span><span>method</span><span>="post"</span><span> action</span><span>="upload.php"</span><span> enctype</span><span>="multipart/form-data"</span><span>></span><span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="MAX_FILE_SEZE"</span><span> value</span><span>="2000000"</span><span>></span><span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file"</span><span> value</span><span>="view"</span><span>></span><span><</span><span>input </span><span>type</span><span>="submit"</span><span> value</span><span>="upload"</span><span> name</span><span>="B1"</span><span>></span><span></</span><span>form</span><span>></span><span></</span><span>body</span><span>></span><span></</span><span>html</span><span>></span>
登入後複製

upload.php

<?<span>php
</span><span>include</span>("check.php"); <span>//</span><span> 引入自定义函数文件</span><span>$type</span> = <span>array</span>("jpg", "gif", "bmp", "jpeg", "png"<span>);
</span><span>//</span><span> 判断上传文件类型</span><span>$fileext</span> = <span>strtolower</span>(fileext(<span>$_FILES</span>['file']['name'<span>]));
</span><span>$uploadfilename</span> = random(8<span>);
</span><span>if</span>(<span>in_array</span>(<span>$fileext</span>, <span>$type</span><span>)){
    </span><span>$filename</span> = <span>explode</span>(".", <span>$_FILES</span>['file']['name'<span>]);

        </span><span>if</span>(<span>is_uploaded_file</span>(<span>$_FILES</span>['file']['tmp_name'<span>])){
</span><span>//</span><span>        echo $_FILES['file']['tmp_name'];</span><span>$flag</span> = <span>move_uploaded_file</span>(<span>$_FILES</span>['file']['tmp_name'], "/Library/WebServer/Documents/test/".<span>$uploadfilename</span>.".".<span>$fileext</span><span>);
        </span><span>if</span>(<span>$flag</span><span>){
            </span><span>echo</span> "上传成功!"<span>;
        }</span><span>else</span><span>{
            </span><span>echo</span> "Error."<span>;
        }
        </span><span>echo</span> "<a >Back</a>"<span>;
    }
}</span>
登入後複製

check.php

<?<span>php
</span><span>header</span>("Content-type:text/html;charset=utf8"<span>);
</span><span>//</span><span> 获取文件后缀名函数</span><span>function</span> fileext(<span>$filename</span><span>){
    </span><span>$sTemp</span> = <span>strrchr</span>(<span>$filename</span>, "."<span>);
    </span><span>return</span><span>substr</span>(<span>$sTemp</span>, 1<span>);
}
</span><span>function</span> fileext2(<span>$filename</span><span>){
    </span><span>$sTemp</span> = <span>explode</span>(".", <span>$filename</span><span>);
    </span><span>return</span><span>$sTemp</span>[<span>count</span>(<span>$sTemp</span>)-1<span>];

}
</span><span>//</span><span> 生成随机文件名函数</span><span>function</span> random(<span>$length</span><span>){
    </span><span>$captchaSource</span> = "0123456789abcdefghijklmnopqrstuvwxyz这是一个随机打印输出字符串的例子"<span>;

    </span><span>$captchaResult</span> = "2015"; <span>//</span><span> 随机数返回值</span><span>$captchaSentry</span> = ""; <span>//</span><span> 随机数中间变量</span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>$length</span>;<span>$i</span>++<span>){
        </span><span>$n</span> = <span>rand</span>(0, 35); <span>#</span><span>strlen($captchaSource));</span><span>if</span>(<span>$n</span> >= 36<span>){
            </span><span>$n</span> = 36 + <span>ceil</span>((<span>$n</span>-36)/3) * 3<span>;
            </span><span>$captchaResult</span> .= <span>substr</span>(<span>$captchaSource</span>, <span>$n</span>, 3<span>);
        }</span><span>else</span><span>{
            </span><span>$captchaResult</span> .= <span>substr</span>(<span>$captchaSource</span>, <span>$n</span>, 1<span>);
        }
    }
    </span><span>return</span><span>$captchaResult</span><span>;
}
</span>?>
登入後複製

將三個檔案整合成一個:

<?<span>php
</span><span>//</span><span> 获取文件后缀名函数</span><span>function</span> fileext(<span>$filename</span><span>){
    </span><span>$sTemp</span> = <span>strrchr</span>(<span>$filename</span>, "."<span>);
    </span><span>return</span><span>substr</span>(<span>$sTemp</span>, 1<span>);
}
</span><span>function</span> fileext2(<span>$filename</span><span>){
    </span><span>$sTemp</span> = <span>explode</span>(".", <span>$filename</span><span>);
    </span><span>return</span><span>$sTemp</span>[<span>count</span>(<span>$sTemp</span>)-1<span>];

}
</span><span>//</span><span> 生成随机文件名函数</span><span>function</span> random(<span>$length</span><span>){
    </span><span>$captchaSource</span> = "0123456789abcdefghijklmnopqrstuvwxyz这是一个随机打印输出字符串的例子"<span>;

    </span><span>$captchaResult</span> = "2015"; <span>//</span><span> 随机数返回值</span><span>$captchaSentry</span> = ""; <span>//</span><span> 随机数中间变量</span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>$length</span>;<span>$i</span>++<span>){
        </span><span>$n</span> = <span>rand</span>(0, 35); <span>#</span><span>strlen($captchaSource));</span><span>if</span>(<span>$n</span> >= 36<span>){
            </span><span>$n</span> = 36 + <span>ceil</span>((<span>$n</span>-36)/3) * 3<span>;
            </span><span>$captchaResult</span> .= <span>substr</span>(<span>$captchaSource</span>, <span>$n</span>, 3<span>);
        }</span><span>else</span><span>{
            </span><span>$captchaResult</span> .= <span>substr</span>(<span>$captchaSource</span>, <span>$n</span>, 1<span>);
        }
    }
    </span><span>return</span><span>$captchaResult</span><span>;
}

</span><span>$type</span> = <span>array</span>("jpg", "gif", "bmp", "jpeg", "png"<span>);
</span><span>//</span><span> 判断上传文件类型</span><span>$fileext</span> = <span>strtolower</span>(fileext(<span>$_FILES</span>['file']['name'<span>]));
</span><span>$uploadfilename</span> = random(8<span>);
</span><span>if</span>(<span>in_array</span>(<span>$fileext</span>, <span>$type</span><span>)){
    </span><span>$filename</span> = <span>explode</span>(".", <span>$_FILES</span>['file']['name'<span>]);

    </span><span>if</span>(<span>is_uploaded_file</span>(<span>$_FILES</span>['file']['tmp_name'<span>])){
</span><span>//</span><span>        echo $_FILES['file']['tmp_name'];</span><span>$flag</span> = <span>move_uploaded_file</span>(<span>$_FILES</span>['file']['tmp_name'], "/Library/WebServer/Documents/test/".<span>$uploadfilename</span>.".".<span>$fileext</span><span>);
        </span><span>if</span>(<span>$flag</span><span>){
            </span><span>echo</span> "上传成功!"<span>;
        }</span><span>else</span><span>{
            </span><span>echo</span> "Error."<span>;
        }
        </span><span>echo</span> "<a >Back</a>"<span>;
    }
}

</span>?>

<html>
<head>
    <meta http-equiv="content-type" c charset="utf-8">
    <title>Upload Image</title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SEZE" value="2000000">
    <input type="file" name="file" value="view">
    <input type="submit" value="upload" name="B1">
</form>
</body>
</html>
登入後複製

以上就介紹了php上傳功能集後綴名判斷和隨機命名,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!