php - 简单的视频上传网站,从 PHP 处理程序上传的文件未镜像到 Nginx(Docker - Nginx + PHP-FPM FastCGI)
问题描述
我有一个班级项目,我在其中创建了 docker 容器来托管一个将使用 PHP 进行后端逻辑的网站。目前,我的 NGINX 容器配置为使用 PHP-FPM 容器作为它的 PHP 处理程序。
我遇到的问题是我实际上传视频文件的地方。基本上,当我(网站上的用户)使用调用 PHP 代码move_uploaded_file
的上传框上传视频文件时,可以在 PHP 容器内的“上传”文件夹中看到该文件。当我转到 NGINX 容器时,该文件不存在。我不知道我是否缺少某种配置选项,从 PHP 代码创建的文件将被镜像到 NGINX 文件目录中?
如果我重新启动容器,该文件现在似乎也出现在 NGINX 容器中。问题是当我尝试通过网站查看文件时,(嵌入标签),我得到一个 403 错误。我认为这与 PHP 上传文件的用户有关?
所以基本上,我可以上传一个 PHP 处理程序处理得很好的文件。但是我必须重新启动所有容器才能让它出现在网络服务器上。然后我必须打开一个终端,弄乱权限,通常给它chmod 777
让它玩。我想知道我做错了什么吗?
谢谢,这是一些相关的代码。
<?php
session_start();
include_once("common.php");
date_default_timezone_set('US/Eastern');
ini_set("upload_max_filesize","50000000");
ini_set("post_max_size", "50000000");
if(isset($_POST['submit'])){
#echo 'Current script owner: ' . get_current_user();
$allowedExts = array("mp4", "mpg", "mpeg", "mov", "avi", "flv", "wmv");
$name = $_FILES['file']['name'];
$temp = $_FILES['file']['tmp_name'];
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (in_array($extension, $allowedExts)){
if (file_exists("uploaded/".$name)){
echo $name . " already exists";
}
else{
$moved = move_uploaded_file($temp,"uploaded/".$name);
#exec(chown)
#chown($_SERVER['DOCUMENT_ROOT'].'/uploaded/'.$name,'root');
#chgrp($_SERVER['DOCUMENT_ROOT'].'/uploaded/'.$name,'root');
echo $_SERVER['PHP_AUTH_USER'];
chmod('uploaded/'.$name, 777);
#exec('chmod 777 '.$_SERVER['DOCUMENT_ROOT'].'/uploaded/'.$name);
#if ( $moved ){
#echo "Successfully uploaded"; For debugging
#}
#else{
#echo "Not uploaded because of error #".$_FILES['file']['error']; For debugging
#}
$url = "http://localhost/uploaded/$name";
#echo($_SERVER['DOCUMENT_ROOT'].'/uploaded/'.$name);
if($statement = $mysqli->prepare("SELECT UserID from users where Username=?")){
if($statement->bind_param("s",$_SESSION['user'])){
#echo($_SESSION['user']);
if(!$statement->execute()){
die("Error - Issue executing prepared statement: " . mysqli_error($mysqli));
}
if($res = $statement->get_result()){
$row = $res->fetch_assoc();
if($res->num_rows != 1){
#echo($_SESSION['user']);
die("False - User could not be found???");
}
else{
$userID = $row['UserID'];
#echo($userID);
}
}
$currDate = strval(date("M,d,Y h:i:s A"));
if($statement = $mysqli->prepare("INSERT INTO video (UserID, URL, Name, UploadDate) VALUES(?,?,?,?)")){
if($statement->bind_param("isss", $userID, $url, $name, $currDate)){
#echo($statement);
if(!$statement->execute()){
die("Error - Issue executing sql video upload" . mysqli_error($mysqli));
}
else{
header( "Location: /upload.php?message=File%20uploaded%3A%20".$name);
}
}
}
}
}
chmod($_SERVER['DOCUMENT_ROOT'].'/uploaded/'.$name, 777);
}
}
}
server {
listen 80;
#listen 443 ssl http2;
#listen [::]:443 ssl http2;
server_name localhost;
root /usr/share/nginx/html;
#ssl_certificate /etc/nginx/conf.d/armbook.crt;
#ssl_certificate_key /etc/nginx/conf.d/armbook.key;
#ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#ssl_ciphers HIGH:!aNULL:!MD5;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
client_max_body_size 100M;
access_log /var/log/nginx-access.log;
location / {
root /usr/share/nginx/html;
index index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
fastcgi_param PHP_VALUE "upload_max_filesize=128M \n post_max_size=128M";
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
解决方案
推荐阅读
- typescript - TypeScript 可选属性赋值初始化
- ios - 扩展 JSONDecoder 以允许基于端点以不同方式序列化模型
- javascript - 您将如何使用 map 从对象数组中的每个对象中查找特定值
- cplex - Cplex:无法获取决策变量的值
- ruby - 使用 RVM 安装 ruby 时的编译器问题:检查 C 编译器是否工作...否
- javascript - 在 Heroku 上部署 Node.js 应用程序时出现 403 错误
- perl - perl:同时使用 map 和 foreach?
- reactjs - 单击链接时,activeClassName 不起作用
- google-cloud-platform - Google Cloud IoT Core,通过 SSH 连接到设备
- angular - 日期选择器变量