PHP output messages while script in process - php

For example i'm looping through a big file, and after counter reaches 1000 parsed strings i need to echo message, that 1000 string have been parsed and calculate % of overall completed strings.
Is it possible to make something like that with output buffer?

Take a look at flush(). Whether or not your browser will render the incomplete page, or wait till it finishes loading is entirely implementation-dependent, though...

Make your script to write the progress data in a text file on the server. Now program your webpage with help of Ajax to send request to that file in particular intervals of time. Get the data and calculate the percentage and modify the HTML of your page.

One possiblity is to use another script to output the progress, and have the client poll it in set intervals for current progress, and only ask for the complete output after the whole process is complete.

Related

Display Live Progress and Any Errors During Long Script Execution in CodeIgniter

I'm working on an app that creates QR codes and renders them onto multiple graphics for a user.
The Problem:
I wrote a script to import users to create from a CSV. I'm needing to create over 100 users (each including the process above). Right now it takes roughly 1 minute to complete for each new user to complete the processing.. then spits out all my error/success messages at once.
My Question:
Rather than the browser slowly loading the result view (currently stays on a white page until complete) as my script is processing, is their a somewhat easy way to display the live progress and errors as they happen? Something like a progress bar updated as each user is created/fails. I'm guessing it will require AJAX?
When dealing with websites, remember the golden rule.
PHP MUST DIE.
Noobs assume this is people rubbishing PHP. It isn't. It's the HTTP request cycle.
Request In > PHP > Response Out > PHP process dies.
This is only the case when dealing with web servers and browsers, not CLI PHP. But the point is that you may end up getting Apache timeouts if your script takes as long as you say.
One solution could be to set up a cron that checks for a file and if it finds it, processes it, dumping a line number in a text file that your browser could check, which means you could fetch progress:
<?php
if (file_exists('/some/csv/to/process.csv')) {
// open file
// get row to work on
// process row
// update progress file with next line number
}
Meanwhile, you could set up a script that does this:
<?php
$progress = file_get_contents('/path/to/progress.txt');
header('Content-Type: application/json');
echo json_encode(['progress' => $progress]);
And then get the progress using AJAX inside a setInterval function:
$.get('/path/to/progress/json/page', function(data){
console.log(data);
});
Just an idea, may or may not suit you but give it a try!

Run background job and display results to the user

I have this php script that imports each row to my database. After it saves, in the same process it will do some more stuff, takes 1-2 sec for each row so my user prefers to send the CSV file to me, then I run in the terminal.
I want to build a page to him, so he can drop the CSV in a file input, until here ok, I saw how to make this run in background, but I really want to show in the page the results of importing, my scripts echoes the result for each row in the CSV in the terminal. How to do something similar in the HTML?
How to make the user drops the CSV in a file input and see the result for each row right bellow as like he had executed the script in a terminal on his screen?
EDIT (making things clear)
I want to display the script messages while it's running, I mean, for each row of the csv file, my script will output/echo the result of the import, I want to immediately display this row result to the user in the HTML page. Not after the script execution finishes. Something like a stream.
Sounds like you need to look at using jQuery and AJAX which will achieve what you're aiming for. This allows you to send data to a script, such as PHP, and the script processes in the background before sending the response.

Update Jquery progress bar, while reading txt file (from php)

I have a 100.000 rows txt file and I need to read it in order to insert most part of it into my DB.
I'd like to use this plugin, as I found it very easy to use:
http://www.bram.us/projects/js_bramus/jsprogressbarhandler/#download
My problem is: I read the txt file with PHP, but I don't understant how to update the progress bar!
I was thinking something like this
echo '$("#progressbar").progressbar({ value: '.($k++).' });';
where $k goes from 0 to 100, but, WHERE do I have to put it??
I've found this method:
http://spidgorny.blogspot.it/2012/02/progress-bar-for-lengthy-php-process.html
I think that this could help with some editing to the code.
You can't possibly mix the php and the javascript:
The PHP will run and generate an HTML/JS file
The HTML/JS file will be sent to the client
The client will run the JS: $("#progressbar").progressbar({ value: XX });
So k will be static.
--
If you really want to do something like that easily, you could use an intermediary DB table with three columns: txt_file, position, length
And often update this table while the PHP script is running over a txt file.
Client side, in Javascript, you can make an ajax request using jQuery for example every 5 or 10seconds, which is going to call an other PHP page, and this PHP page will only return the corresponding row from the intermediary table. Once you have the result you can update the progressbar.
--
It's the simplest solution to implement for you, but it's still really dirty, and the parsing of the txt file better have to be really long !
There is no direct method to achieve this thing. PHP script executes first and then the output is sent to the client viewing the web page that is why you cannot show live status of your PHP script's processing to the client.
You will have to use a combination of AJAX and Database :
Create a table to track the progress of the loading of the text file. Whenever a user (client) sends a request to the page, keep on updating the table with the progress. Use Session id as the index on the table so that it would be easy to track the progress per client. Now use AJAX requests to get the progress from the table and present it to the client with your progress bar.

Waiting for xml file writing to finish in PHP

My PHP knowledge is limited and I'm having a problem with a script I am running.
I am amalgamating quite a few xml feeds and writing the output to my own server in a custom xml file.
I am using XMLWriter to do this, but the problem I am having is knowing when the file has successful finished being written to?
I am loading the external feeds via SimpleXMLElement, and in total the script takes around 10 seconds to run, but when I do a print_r($xmlWriter) at the end of the script it is empty, so too is the xml file Im trying to write.
The xml file is successfully written, but in the content of php im not sure when and would like only to proceed to some other code once this is successful.
Any help appreciated
Thanks
You could check if $xmlWriter is empty, as you say this is the problem, and loop until it is not empty, but at the same time you can tell PHP to wait a while to save performance:
//while the XML hasn't finished being written yet
while ($xmlWriter == "") {
// sleep for 10 seconds
sleep(10);
}
You could even keep a count of how long it waits for and give up after a while and handle the failure.

Ouput stuff while php script is running

So I just created a script to resize a whole bunch of images. Is there anyway to have there be output as its running through the loop?
Basically I have like 400 photos in photo db table. Its gathering a list of all these photos, then looping through each one and resizing it 3 times. (large,medium,small version).
Right now on each loop I am echoing that images results, but I dont see the results untill EVERYTHING is done. So like 10 minutes later, I will get output. I added this set_time_limit(0); to make sure it doesnt time out.
**EDIT ** It looks like every so often the script actually updates to the browser, maybe every 30 seconds?
You can use flush() or ob_flush() to tell the script to send something back to the client after you do an echo().
BUT - you never really have complete control over it, the web server does, so the web server may not cooperate depending on how its configured. For example, if you have the server doing gzip rather than using PHP's gzip features, the web server may still buffer the output.

Categories