验证文件名的最佳业务实践是什么?

发布于 2024-08-14 04:59:41 字数 1461 浏览 8 评论 0原文

我有一个“困境”,想知道什么是商业最佳实践。

我正在使用 Uploadify 上传图像。现在我需要在保存文件之前验证文件名。

我研究了不同的解决方案,但无法找到一种好的解决方案。

以下是我的标准:

  • 文件名必须全部小写
  • 文件名只能包含字符 [a-z0-9_-]
  • 我必须能够重命名文件

如果文件名是 my.file(name),您会如何处理。 jpeg ?

我可以在“.”上分解文件名并保存扩展名,然后内爆以再次获取文件名。但不确定这是否是最好的解决方案。

我有以下功能可以提供一些帮助:

function getExts($filename) 
{ 
    $exts = explode("[/\\.]", $filename) ; 
    $n = count($exts)-1; 
    $exts = $exts[$n]; 
    return $exts; 
}

function validFilename($filename)
{
    $filename = str_replace(" ", "_", $filename);
    $pattern = "/[^[a-z0-9_-]/";
    return preg_replace($pattern, "", strtolower($filename));
} 

更新 1
我通过 $_FILES 接收文件。这给了我以下数据:

  • $_FILES["file"]["name"] - 上传文件的名称
  • $_FILES["file"]["type"] - 上传文件的类型
  • $_FILES["file "]["size"] - 上传文件的大小(以字节为单位)
  • $_FILES["file"]["tmp_name"] - 存储在服务器上的文件临时副本的名称
  • $_FILES["file"][ "error"] - 文件上传产生的错误代码

UPDATE 2
我刚刚发现了一些东西。我可以使用 getimagesize 它将返回一个包含 7 个元素的数组。这些元素 [2] 之一是 IMAGETYPE_XXX。

所以我尝试使用这段代码:

function getExts2($filename)
{
    list(,,$type) = getimagesize($filename);
    return $type;
}

但它只返回数字2...

(我也尝试使用exif_imagetype,但它只得到PHP错误:调用未定义的函数。)

I have a "dilema" and wonder what is business best practice.

I'm using Uploadify to upload images. Now I need to validate the filename before saving the file.

I've looked at different solutions, but can't get down to one good solution.

Here are my criterias:

  • Filename must be all in lowercase
  • Filename can only contain charaters [a-z0-9_-]
  • I must be able to rename file

How would you go about if a filename is my.file(name).jpeg ?

I could explode the filename on '.' and save the extension, then implode to get the filename again. But not sure if that's the best soltion.

I have the following functions that helps a bit:

function getExts($filename) 
{ 
    $exts = explode("[/\\.]", $filename) ; 
    $n = count($exts)-1; 
    $exts = $exts[$n]; 
    return $exts; 
}

function validFilename($filename)
{
    $filename = str_replace(" ", "_", $filename);
    $pattern = "/[^[a-z0-9_-]/";
    return preg_replace($pattern, "", strtolower($filename));
} 

UPDATE 1
I'm recieving the file through $_FILES. This gives me the following data:

  • $_FILES["file"]["name"] - the name of the uploaded file
  • $_FILES["file"]["type"] - the type of the uploaded file
  • $_FILES["file"]["size"] - the size in bytes of the uploaded file
  • $_FILES["file"]["tmp_name"] - the name of the temporary copy of the file stored on the server
  • $_FILES["file"]["error"] - the error code resulting from the file upload

UPDATE 2
I just found something. I could use getimagesize which will return an array of 7 elements. One of these elements [2] is IMAGETYPE_XXX.

So I try using this code:

function getExts2($filename)
{
    list(,,$type) = getimagesize($filename);
    return $type;
}

But it only returns the number 2...

(I also tried using exif_imagetype, but it only get PHP Error: Call to undefined function.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

莫多说 2024-08-21 04:59:41

使用正则表达式检查文件名。使用有关 mimetype 的信息。使用 md5 名称将文件保存在服务器上。将真实文件名存储在数据库上。

Check filename with regexp. Use info about mimetype. Save file on server with md5 name. Store real filename on db.

暮倦 2024-08-21 04:59:41

pathinfo() 可以获取文件名和扩展名。不过,我会警告您,您不能依赖通过检查文件名来测试文件的扩展名。您将需要使用一个函数来实际检查文件的二进制内容。 finfo_file() 可以完成此操作。哦,在用户提供的文件路径上使用 basename() 来阻止路径也没什么坏处遍历攻击。

pathinfo() can get you the filename and extension. I'll warn you that you can't rely on testing a file's extension through inspection of its filename, however. You will want to use a function that actually inspects the binary contents of a file for this. finfo_file() can accomplish this. Oh, and it never hurts to use basename() on a user-supplied file path to prevent a path traversal attack.

末蓝 2024-08-21 04:59:41

我假设您正在验证文件名以供以后用作下载文件名。

虽然为了用户的利益保留文件名是很好的,但从技术角度来看,您的方法听起来不错。

如果您关心 URL 的外观,并且将来可能面向国际受众,请务必将变音符号转换为其基本字符或将其转换。德国用户(变音符号 ä Ö Ü ß)会期望转换:

Ä = ae
Ö = oe
Ü = ue

感到放心

Ä => a
Ö => o
Ø => o

而斯堪的纳维亚人似乎对等等

。然后还有各种重音字符 é á ó...

完全删除这些字符会导致在 bwsr 地址 br 中看起来真正 bd 和 lok strng 的 URL。

I am assuming you are validating file names for later use as download file names.

While it would be great to preserve file names as they are for the sake of the user, from a technical viewpoint, your approach sounds sound.

If you care about what the URLs look like,and may target an international audience in the future, be sure to either convert Umlauts to their base characters or to convert them. A german user (Umlauts Ä Ö Ü ß) would expect conversion:

Ä = ae
Ö = oe
Ü = ue

while the scandinavians seem to be at ease with

Ä => a
Ö => o
Ø => o

and so on.

Then there are the various accented characters é á ó...

Dropping those altogether leads to URLs that look really bd and lok strng in the bwsr address br.

成熟稳重的好男人 2024-08-21 04:59:41

这应该可以完成您想要的一切:

$filename = preg_replace('/[^[a-z0-9_-]/', '', str_replace(' ', '_', strtolower(pathinfo($filename, PATHINFO_FILENAME)))) . pathinfo($filename, PATHINFO_EXTENSION);

正如 Pekka 所解释的,如果您想替换文件名中的所有重音符号,您可以使用以下函数:

function Unaccent($string)
{
    return preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8'));
}

This should do everything you want:

$filename = preg_replace('/[^[a-z0-9_-]/', '', str_replace(' ', '_', strtolower(pathinfo($filename, PATHINFO_FILENAME)))) . pathinfo($filename, PATHINFO_EXTENSION);

And as Pekka explained it, if you want to substitute all accentuation in the file name you can use the following function:

function Unaccent($string)
{
    return preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8'));
}
葵雨 2024-08-21 04:59:41

我认为您不应该“验证”文件名,如果它不是您想要的格式,您应该“修复”它。为什么仅仅因为有人不善于命名文件或其中包含一些不寻常的字符就拒绝文件?另外,如果您正在处理图像,则可以使用您提到的 getimagesize 来确保它确实是已上传的图像(如果不是图像,则应该失败)。

I don't think you should "validate" the filename, you should just "fix" it if it isn't in the format you want. Why reject a file just because someone sucks at naming their files, or has some unusual characters in there? Also, if you're working with images, you can use getimagesize as you mentioned to make sure it actually is an image that was uploaded (should fail if it isn't an image).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文