【问题标题】:Relative path in $_FILE["file"]["name"] truncated to basename?$_FILE["file"]["name"] 中的相对路径被截断为基本名称?
【发布时间】:2017-08-29 00:33:08
【问题描述】:

我想将文件发布到服务器,并在 Content-Disposition 标头中提供文件文件名的相对路径(在带有 curl 7.47 的 Ubuntu 上使用 PHP 7.0):

curl server/index.php -F "file=@somefile.txt;filename=a/b/c.txt"

应用--trace-ascii /dev/stdout 选项显示:

0000: POST /index.php HTTP/1.1
0031: Host: server
004a: User-Agent: curl/7.47.0
0063: Accept: */*
0070: Content-Length: 111511
0088: Expect: 100-continue
009e: Content-Type: multipart/form-data; boundary=--------------------
00de: ----e656f77ee2b4759a
00f4: 
...
0000: --------------------------e656f77ee2b4759a
002c: Content-Disposition: form-data; name="file"; filename="a/b/c.txt
006c: "
006f: Content-Type: application/octet-stream
0097: 
...

现在,我的简单测试脚本<?php print_r($_FILES["file"]); ?> 输出:

Array
(
    [name] => c.txt
    [type] => application/octet-stream
    [tmp_name] => /tmp/phpNaikad
    [error] => 0
    [size] => 111310
)

但是,我期待[name] => a/b/c.txt。我的逻辑缺陷在哪里?

根据https://stackoverflow.com/a/3393822/1647737,文件名可以包含相对路径。

PHP manual also implies this 并建议使用basename() 进行消毒。

【问题讨论】:

  • 我认为由于安全原因,您无法获取上传文件的原始路径。 php 和 javascript 也无法检索此信息。
  • @Wizard 您适合从浏览器中上传;除了旧的 IE 版本,没有浏览器发送完整的本地路径。但是这里的截断必须发生在服务器端(从包含完整路径的跟踪中可以看出)。
  • 是的,我明白了.. 但我怀疑 php 端出于同样的原因将其切断。你可以read the code of php-interpreter
  • @Wizard 感谢您的参考,将阅读它。如果 PHP 确实是罪魁祸首,那么这种行为可能会随着时间的推移而改变,因为手册和其他来源仍然建议使用 basename() 清理(?)提到将来会删除此行为...
  • @Wizard 您想发表您的评论作为答案,以便我接受吗?如果没有,我稍后会参考您的评论写一个答案。感谢您在 PHP 解释器源代码中指向该确切位置的指针!

标签: php curl file-upload path


【解决方案1】:

php-interpreter sources_basename() 过滤器中可以看出,出于安全原因和/或修复某些特定浏览器的缺点。

文件:php-src/main/rfc1867.c

~1151 行及以下:

        /* The \ check should technically be needed for win32 systems only where
         * it is a valid path separator. However, IE in all it's wisdom always sends
         * the full path of the file on the user's filesystem, which means that unless
         * the user does basename() they get a bogus file name. Until IE's user base drops
         * to nill or problem is fixed this code must remain enabled for all systems. */
        s = _basename(internal_encoding, filename);
        if (!s) {
            s = filename;
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-22
    • 1970-01-01
    • 2020-03-31
    • 2018-09-30
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多