php 正在更改原始文件名

php is altering original filename

我有一个文件上传,并尝试通过删除不需要的字符来处理奇怪的文件名。 但是我遇到的情况是我只是玩弄不需要的(字符)文件名并得到以下结果:

我的错误文件名是:1\;ping\ -c1\ 1.1.1.1
Firefox 正在发送:
Content-Disposition: form-data; name="send_file"; filename="1\;ping\ -c1\ 1.1.1.1"

但是 PHP 的 $_FILES['send_file']['name'] 对象只是给我 1.1.1.1(前导 space)作为名称。

一般来说这可以忽略不计,但我想了解这里发生了什么

这里发生了什么?请赐教

使用的软件:

一般来说,如您所知,切勿在未经任何验证的情况下使用 $_FILES['send_file']['name']

无法判断您的 $_FILES['send_file']['name'] 是什么,因为这是由浏览器设置的。因此您的浏览器 pre-processes 文件名并删除任何不需要的字符。


让我们来看看当您 select 文件并提交表单(headers 缩写)时会发生什么:

POST /upload.php HTTP/1.1
Host: localhost:8080
Content-Length: 1234
Origin: http://localhost:8080
[...]
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="send_file"; filename="hello.pdf"
Content-Type: application/x-object

... contents of file goes here ...
------WebKitFormBoundary7MA4YWxkTrZu0gW

在这种情况下,$_FILES['send_file']['name'] 将设置为 hello.pdf


简而言之,PHP 和您的网络服务器(据我所知)都不会对文件名进行任何预处理。它完全由您的用户的操作系统和浏览器设置。

它涉及自 2005 年以来非常古老的问题,基本上字符 \ 不是 Window

中文件名的有效字符

您可以在此处找到更多详细信息,旧错误状态为 不会修复

https://pear.php.net/bugs/bug.php?id=5681

PHP 的 $_FILES['send_file']['name'] 等于 Content-Dispositionfilename="..." 值。

PHP 正在做一些清理:删除 path 并仅存储 文件名,就像它在 documentation:

$_FILES['userfile']['name']

The original name of the file on the client machine.

删除路径意味着最后一个slash/backslash之前的所有内容(包括它们)都被删除,这就是你得到的。