问题描述
我有一个PHP应用程序。我允许用户上传文件到我的web应用程序。
问题:对我上传文件的文件名进行清理的最好方法是什么 $ _ FILES [filename] [tmp_name]
in PHP?
$ b
更新:
我可以取得上传档案的MD5并用它作为新分配的文件名?如果是的话,我该如何做到这一点在PHP?
为了避免文件名冲突只是检查给定或生成的文件名不已存在:
do {
//生成文件名,例如:
$ filename = md5 uniqid())。 $ fileExtension;
} while(file_exists($ filename));
这样可以100%确定文件名是唯一的。使用md5(或任何其他散列算法)可以确保文件名是安全的 - 并且易于处理。
I have a PHP application.
I allow users to upload files to my web application.
Question: What's the best way for me to sanitize the file names of the uploaded documents $_FILES["filename"]["tmp_name"]
in PHP?
UPDATE:
Can I take an MD5 of the uploaded filename and use that as the newly assigned filename? If so, how do I do that in PHP?
To avoid filename collision just check whether given or generated filename doesn't already exists:
do {
// Generate filename, eg.:
$filename = md5(uniqid()) . $fileExtension;
} while (file_exists($filename));
That gives you 100% sure that the filename is unique. Using md5 (or any other hash algorithm) ensures you that the filename is secure - and easy to handle.
这篇关于PHP:如何清理上传的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!