The progress bar for uploading in Docker with Nginx and PHP-fpm is not functioning correctly

Currently, I am employing docker-compose with PHP 7.2 from the phpdockerio/php72-fpm:latest image.

services:
    webserver:
      image: nginx:alpine

The configuration for nginx is as follows in nginx.conf:

user nginx;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /dev/stdout  main;

    #sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip  on;


 server {


     listen 80 default;

     client_max_body_size 208M;

     access_log /var/log/nginx/application.access.log;

     root /application/public;

     rewrite ^/index\.php/?(.*)$ /$1 permanent;

     try_files $uri @rewriteapp;

     location @rewriteapp {
         rewrite ^(.*)$ /index.php/$1 last;
     }

     # Deny all . files
     location ~ /\. {
         deny all;
     }

     location ~ ^/(index)\.php(/|$) {
         fastcgi_pass php-fpm:9000;
         fastcgi_split_path_info ^(.+\.php)(/.*)$;
         fastcgi_index app_dev.php;
         send_timeout 1800;
         fastcgi_read_timeout 1800;
         fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
         fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
         fastcgi_buffers 16 16k;
         fastcgi_buffer_size 32k;
         include fastcgi_params;
     }

     # Statics
         location /(bundles|media) {
         access_log off;
         expires 30d;
         try_files $uri @rewriteapp;
     }}

}

A controller has been developed specifically for updating the progress bar.

 /**
     * @Route("/progressTest", name="progressTest")
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function progressAction(){

        ob_start();
        header('Content-Type: text/event-stream');
        header('Cache-Control: no-cache');

        $total = 10;
        for($i=0;$i<$total;$i++)
        {

            $percent = intval($i/$total * 100)."%";

            sleep(1); // Here call your time taking function like sending bulk sms etc.

            echo '<script>
            parent.document.getElementById("progressbar").innerHTML="<div style=\"width:'.$percent.';background:linear-gradient(to bottom, rgba(125,126,125,1) 0%,rgba(14,14,14,1) 100%); ;height:35px;\">&nbsp;</div>";
            parent.document.getElementById("information").innerHTML="<div style=\"text-align:center; font-weight:bold\">'.$percent.' is processed.</div>";
            </script>';

            ob_flush();
            flush();


        }
        echo '<script>parent.document.getElementById("information").innerHTML="<div style=\"text-align:center; font-weight:bold\">Process completed</div>"</script>';


        return new Response('OK '.$i);


    }

To display this progress bar in the template, use the following code snippet:

  <div class="col-md-12">
                        <div id="progressbar" style="border:1px solid #ccc; border-radius: 5px; "></div>
                        <div id="information" style="border:1px solid #ccc; border-radius: 5px; "></div>
                    </div>
                    <iframe name="loadarea" src="{{ path("progressTest") }}" id="loadarea" style="display:none2;"></iframe><br />

If there are pending updates until the end of the loop, you may encounter issues, such as getting all updates in a single batch. One possible solution could be to set the X-Accel-Buffering header to no. Also, consider restructuring the JavaScript logic to handle graphical manipulation more efficiently based on percentage values.

Answer №1

It appears that you may be overlooking the necessity of a header called X-Accel-Buffering. This header is outlined in the Streaming a Response section of the official documentation. Essentially, your FPM does not store any data, whereas NGINX buffers it, resulting in a bulk delivery at the end.

Attempt setting this header to no and observe if it resolves the issue.

Additionally, returning a <script> tag in this manner is akin to taking unnecessary risks. It would be wise to consider restructuring the code so that client-side manipulation handles graphical representation based on percentage alone.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to have a text field automatically insert a hyphen after specific numbers?

Is there a way to make a text field insert hyphens automatically after certain numbers? For example, when typing a date like 20120212, could we have the input automatically formatted with hyphens after the first 4 digits and the second two, so it displays ...

What happens when an AJAX request doesn't have a success field?

Is it possible to execute an ajax call without specifying a success function? $.ajax({ type: "POST", url: "/project/test/auto", data: data, // no success function defined here }); My reasoning for this is that I have PHP code that insert ...

The ezPublish system is indicating that there are insufficient arguments present in the specified function [(''|ezurl()|ne('/'

We are facing the following issues: Any ideas on how to fix this? Error: Not enough arguments at [(''|ezurl()|ne('/'))] This error is occurring in the specific file 'extension/ezcbdigeventcalendar/design/standard/templates/par ...

simplify sending emails through WAMP and a proxy server

For the past couple of days, I've been attempting to send emails from my PHP application that's running on my PC with WAMP. I made adjustments to the php.ini file as follows: [mail function] ; For Win32 only. ; http://php.net/smtp SMTP = mail.my ...

Querying objects using a PHP API

Currently facing an issue with the API documentation I'm working on. Despite testing the code, it simply isn't functioning as expected. After spending a considerable amount of time analyzing it, I seem to be at a loss in figuring out where the er ...

Utilizing JQuery Ajax in Synchrony with UI Dialogs

I am struggling with creating an ajax pop up when a button is clicked in the order include of my main smarty template. Despite including jquery, jquery ui, and the necessary js file script, the page just reloads instead of displaying the pop-up as intended ...

Retrieving historical data from a MySQL database to display in a div element

My main page features a button that opens a popup. Upon closing the popup, a script is triggered to call a PHP file for selecting data from the database. This selected data is then appended to a specific div within the main page: if (win.closed !== false ...

A guide on transferring files to a PHP server using HttpClient and FormData within an Ionic framework, along with instructions on receiving files in PHP

I have an input file in mypage.html on Ionic and I want to send this file to PHP for uploading it onto the server. In mypage.page.html, I have created an input field for the file name and another input field for the file to be uploaded. <ion-header> ...

Changing password using mysqli in PHP when a fresh one is provided

Being relatively new to PHP and mysqli, I feel like I might be approaching this the wrong way. Maybe I should be checking if the password needs to be updated when it's not the same? However, I'm uncertain about how to go about doing that. Curren ...

PHP array containing images with redirection

There is a file called foo.txt that contains the locations of various pictures. http://foo/bar1.png http://foo/bar2.png http://foo/bar3.png When displaying these pictures using an array, some are missing and the server redirects to the same 404 image for ...

What is the appropriate method for passing parameters in PHP and how can you handle returned empty values

I'm looking to pass parameters in the action method because I am unable to do so via the header. <form name="mailinglist1" method="post" action="report1.php" > In this form, I'm using a download button to connect my report (HTML). ...

What are the advantages of incorporating JSON for retrieving data from MySQL?

So I have developed a website that retrieves data from a MySQL database using PHP, and then transforms it into JSON format for use with JavaScript. This process can be broken down into 5 steps: Query -> PDO Statement -> PHP Array -> JSON Strings -> JS ...

Fetch the information, insert following, and trigger a slide toggle

I am new to jQuery and I want to create a sliding table. The original table has 3 levels: <ul class="categories"> <li id="category1">1 element</li> //parentid=0 <li id="category2">2 element</li> //parentid=0 <ul> < ...

Mastering the Art of Restricting an IP Address Range with FILTER_VALIDATE_IP

In order to restrict access to certain resources, I need to implement a system that filters IP addresses. To validate the IP address, I currently use FILTER_VALIDATE_IP and filter_var. However, I am facing an issue where the starting and ending IP address ...

I'm having no luck with my fetch query, it's coming up empty

This code snippet is set up to retrieve content from the database using a global $connect variable for the database connection, which seems to be functioning correctly. However, I am puzzled as to why the webpage is not displaying any content. Any assistan ...

Utilizing AJAX and PHP for dynamic changes based on listbox selection

Is there a way to make the query run whenever a selection is made from the listbox? if(isset($_POST['select'])){ $newselection=$_POST['select']; $query = "SELECT count(*) FROM listings where description=" . $newselection. ""; $r ...

Load jQuery in PHP script and return it

After searching around, I have not been able to find a suitable example for what I am trying to accomplish. Here is the scenario that I am facing: On a page (constructed in PHP and HTML), there is an included PHP script (currentMonth.php) that runs when th ...

What is the most efficient way to transfer data from PHP Laravel to Vue.js and automatically update a Vue.js component whenever the data is modified?

New to Vue.js and feeling a bit lost about how reactivity works with it. I want to send data from PHP Laravel to a Vue.js component and have the component update automatically when the data changes. I've come across suggestions on Stack Overflow reco ...

The nvm is functioning properly within the bash shell, however, it is not working within the

RUN curl --silent -o- https://raw.githubusercontent.com/creationix/nvm/v0.39.1/install.sh | bash - && \ . $HOME/.nvm/nvm.sh && \ nvm ls && \ chmod -R 777 /bin/ && \ nvm install ...

Creating PopUp Windows with PHP and JavaScript

There is a function created on my page that opens a pop-up window when clicking on a game-mod name: <SCRIPT language="javascript" type="text/javascript"> function popModData( modName ) { var url = "./modList.php?mod=" + modName; ...