【发布时间】:2022-02-19 01:39:11
【问题描述】:
我需要将服务器端tmpfile()创建的文件发送到浏览器。
我正在使用两个不同的 Docker 容器:php 和 nginx(docker compose)。
我的 nginx 配置文件(Docker 容器nginx):
server {
listen 80 default_server;
index index.php index.html index.htm;
root /app;
location ~ [^/]\.php(/|$) {
autoindex on;
autoindex_exact_size on;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
error_log stderr warn;
access_log /dev/stdout main;
}
我的 PHP 代码(Docker 容器php):
<?php
$file = tmpfile();
fwrite($file, "hello world");
$metaData = stream_get_meta_data($file);
$fileName = $metaData['uri'];
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Content-type: text/plain');
header('Content-Length: ' . filesize($fileName));
header('X-Sendfile: ' . $fileName);
fclose($file);
但是浏览器只得到一个空页面而不是下载弹出窗口。
【问题讨论】:
-
您无法使用
tmpfile()来完成此操作。您是否需要详细回答为什么以及可以建议什么? -
@IvanShatsky 为什么 tmpfile() 不可能?还有什么替代方案?
标签: php docker nginx docker-compose