Run codes and print the result

Hello,
I want print result of the running code
Like this ( example ) :

<?php
mkdir('folder1');
echo 'folder1 created';
echo 'dowloading large file, please wait...';
copy('FileLink', 'LargeFile.x');
echo 'the large file downloaded successfully';
// other codes..
?>

if run this code, It takes a long time and shows the result in one place. I want to print all the results gradually, just like the Linux terminal. How can I do it?

If you run this code on the terminal it will print one line at a time as you want.

If you run it through a browser it won’t; the PHP server will finish running the script before returning the output, which means there’s never a step when the browser can only see part of the output.

You can get around this by using polling or websockets, but these are complicated solutions.

Alternately… you could run an echo line…

*Then do a function/check for the dir and/or the file if it exists…(after each step)… then print out the results.

Is not there another solution? JavaScript, Ajax or anything else

I know this. I want the result to be printed step by step in the browser as the code is executed

You’d use JavaScript to do the polling solution. You’d need an API to start the process, and another one to check how far along the process is. Your JavaScript would call the start API, then repeatedly call the progress API to see how the process is doing.

Then you dont understand how the browser works
it takes the complete output before sending you the results.
so your options are using javascript or constantly refreshing the page
and in order for you to constantly refresh the page you would need to know up to what point to continue and store the output of previous calls to this page.

Sponsor our Newsletter | Privacy Policy | Terms of Service