PHP:如何清理上传的文件名? [英] PHP: How to sanitize uploaded filenames?

查看:206
本文介绍了PHP:如何清理上传的文件名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个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:如何清理上传的文件名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆