可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a php script that run 2 exec.
exec(".....", $output, $return1); echo $return1; exec(".....", $output, $return2); echo $return2;
This 2 exec take 1 minute each to run. When I run this script, I'm waiting 2 minutes and it result "00". It's OK but I would like to see $return1
; after 1 minute and see $return2
after 1 minute. I have to use Ajax / js ? In fact, I would make a progress bar, when first exec is done => 50% when second exec is done => 100%
Thank a lot for your help!
Start solution :
Something like this :
define.php :
setInterval(function() { $.ajax({ type: "GET", url: 'convert.php?file='+id_file, cache:false, success:function(result){ $('.content_box').html(result); } }); }, 3000);
Convert.php :
exec(convert id_file.pdf id_file.jpg, $output, $return1); echo $return1; exec(convert -resize50x50 id_file.jpg id_file_thumb.jpg, $output, $return2); echo $return2;
But it run without callback and non-stop!
回答1:
Yes, you have to use AJAX - just keep your script as is, and you will need another script that reports the progress and/or partial results of the original script. The original script can use $_SESSION to pass the progress / partial results information, that's the simplest solution.
Or, you could use AJAX request not just to report progress/partial results, but to run the action stage by stage, as Zar is probably proposing. But this approach is less flexible; there might be some global data that the stages share, so it is better if they are both within one script run. So the approach I sketched in the 1st paragraph is more viable.
回答2:
You could do it in javascript and AJAAX, indeed. Look into setTimeout()
and AJAX.
You could specify GET or POST data so the server know what function to run.
回答3:
There isn't any way to control When PHP returns the results. The best option would to exec one of them, then return that. Once the first one has run set it to then run the second one.
回答4:
If the exec()
'ed apps output progress information, you can even get this: Classically we do this with a redirection trick:
First Create a shell script like
#!/bin/bash $OUTFILE=$1 shift your_executable "$@" > $OUTFILE & echo $!
then
- Create (closed) temp file
exec()
the script with temp filename as first parameter and store the returned PID - Now poll the temp file, whenever you find new lines
echo
them and fflush()
- When the process with the stored pid has exited, exit your script