PHP 和 lighttpd:无法访问 POST 数据

PHP 和 lighttpd:无法访问 POST 数据

我在 Raspbian 上运行带有 lighttpd/1.4.53 的 PHP 7.2 fpm。我的目标是通过 HTTP POST 上传图像文件 - 但无论我向脚本发布什么,$_POST、$_FILES、$_REQUEST 超全局变量都只是空数组。但是,$_SERVER 返回一个正确填充的数组。如果在 URL 中添加任何参数,$_GET 也是如此。

这是我用来测试的:

<?php
$outhandle = fopen("logfile.txt", "a");
fputs($outhandle, print_r($_FILES, true) . "\n");
fputs($outhandle, print_r($_REQUEST, true) . "\n");
fputs($outhandle, print_r($_GET, true) . "\n");
fputs($outhandle, print_r($_POST, true) . "\n");
fputs($outhandle, print_r($_SERVER, true) . "\n");

结果如下:

Array
(
)
Array
(
)
Array
(
)
Array
(
)
Array
(
    [USER] => www-data
    [HOME] => /var/www
    [CONTENT_TYPE] => image/jpeg
    [HTTP_CONTENT_LENGTH] => 2879609
    [HTTP_CONNECTION] => keep-alive
    [HTTP_ACCEPT_ENCODING] => gzip, deflate, br
    [HTTP_HOST] => 192.168.10.2
    [HTTP_POSTMAN_TOKEN] => dfeea482-03cf-4b87-b088-3e727d9ed537
    [HTTP_ACCEPT] => */*
    [HTTP_USER_AGENT] => PostmanRuntime/7.26.1
    [REMOTE_PORT] => 54820
    [REMOTE_ADDR] => 192.168.10.162
    [SERVER_NAME] => 192.168.10.2
    [SERVER_ADDR] => 192.168.10.2
    [SERVER_PORT] => 80
    [REQUEST_SCHEME] => http
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_SOFTWARE] => lighttpd/1.4.53
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => POST
    [DOCUMENT_ROOT] => /var/www/html
    [SCRIPT_FILENAME] => /var/www/html/upload.php
    [SCRIPT_NAME] => /upload.php
    [REDIRECT_STATUS] => 200
    [REQUEST_URI] => /upload.php
    [QUERY_STRING] =>
    [CONTENT_LENGTH] => 2879609
    [FCGI_ROLE] => RESPONDER
    [PHP_SELF] => /upload.php
    [REQUEST_TIME_FLOAT] => 1593803808.1838
    [REQUEST_TIME] => 1593803808
)

请注意,CONTENT_LENGTH 约为 2.8M(也尝试过更小的文件,结果相同),这是我通过 Postman 发送的 POST 请求。我在网上找到了一些建议,它们都涉及 php.ini 文件中的设置,但没有一个能解决问题:

upload_max_filesize = 8M
max_file_uploads = 20
request_order = "GP"
variables_order = "GPCS"

我猜想可能是某种配置错误,但我不知道我错过了什么?以下是 lighttpd 配置文件:

server.modules = (
        "mod_indexfile",
        "mod_access",
        "mod_alias",
        "mod_redirect",
)

server.document-root        = "/var/www/html"
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"
server.pid-file             = "/var/run/lighttpd.pid"
server.username             = "www-data"
server.groupname            = "www-data"
server.port                 = 80

# strict parsing and normalization of URL for consistency and security
# https://redmine.lighttpd.net/projects/lighttpd/wiki/Server_http-parseoptsDetails
# (might need to explicitly set "url-path-2f-decode" = "disable"
#  if a specific application is encoding URLs inside url-path)
server.http-parseopts = (
  "header-strict"           => "enable",# default
  "host-strict"             => "enable",# default
  "host-normalize"          => "enable",# default
  "url-normalize-unreserved"=> "enable",# recommended highly
  "url-normalize-required"  => "enable",# recommended
  "url-ctrls-reject"        => "enable",# recommended
  "url-path-2f-decode"      => "enable",# recommended highly (unless breaks app)
 #"url-path-2f-reject"      => "enable",
  "url-path-dotseg-remove"  => "enable",# recommended highly (unless breaks app)
 #"url-path-dotseg-reject"  => "enable",
 #"url-query-20-plus"       => "enable",# consistency in query string
)

index-file.names            = ( "index.php", "index.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

# default listening port for IPv6 falls back to the IPv4 port
include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.conf.pl"
include "/etc/lighttpd/conf-enabled/*.conf"

#server.compat-module-load   = "disable"
server.modules += (
        "mod_compress",
        "mod_dirlisting",
        "mod_staticfile",
)

答案1

我意识到这不是 lighttpd 的问题。事实上,这些超全局变量仅在通过 Web 表单提交 POST 请求时才会填充。要从其他请求获取已发布的文件,必须从 php://input 读取内容,如下所示:

file_put_contents($path_to_destination_file, file_get_contents('php://input'));

虽然我不知道这应该如何处理多个文件 - 但这不是我现在遇到的问题。

相关内容