Simple video uploading website, file uploaded from PHP handler not mirrored onto Nginx (Docker – Nginx + PHP-FPM FastCGI)

I have a class project where I have created docker containers to host a website that will be using PHP for backend logic. Currently, my NGINX container is configured to use the PHP-FPM container as it’s PHP handler.

The problem I am running into is where I am actually uploading the video file. Basically, when I (user on the website) upload the video file, using an upload box that calls PHP code move_uploaded_file the file can be seen in the ‘uploaded’ folder inside the PHP container. When I go to the NGINX container, the file is not there. I don’t know if I am missing some sort of configuration option where files created from PHP code will get mirrored onto the NGINX file directory?

If I restart the containers, the file now seems to show up in the NGINX container as well. The problem is when I try to view the file through the website, (embed tag), I get a 403 error back. I assume this has something to do with the user that PHP uploaded the file as?

So basically, I can upload a file which the PHP handlers deals with just fine. But I have to restart all the containers just for it to show up on the webserver. Then I have to open a terminal, and mess with the permissions, usually giving it chmod 777 to get it to play. I am wondering if there is something I am doing wrong?

Thanks and here is some of the relevant code.

<?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;
#}

}

Source: StackOverflow