我有一个课堂项目,我在其中创建了docker容器来托管将使用PHP进行后端逻辑的网站。目前,我的NGINX容器已配置为使用PHP-FPM容器作为其PHP处理程序。
我遇到的问题是我实际上在上传视频文件。基本上,当我(网站上的用户)上传视频文件时,使用一个调用PHP代码move_uploaded_file
的上传框,可以在PHP容器内的“已上传”文件夹中看到该文件。当我转到NGINX容器时,文件不存在。我不知道是否缺少某种配置选项,将从PHP代码创建的文件镜像到NGINX文件目录?
如果我重新启动容器,该文件现在似乎也将显示在NGINX容器中。问题是,当我尝试通过网站(嵌入标签)查看文件时,返回了403错误。我认为这与PHP上传文件的用户有关吗?
因此,基本上,我可以上载PHP处理程序可以处理的文件。但是我必须重新启动所有容器才能使其显示在Web服务器上。然后,我必须打开一个终端,并弄乱权限,通常给它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;
#}
}