Using a php to copy a dir and when finished to redirect browser ie
1. print "wait" message in browser.
2. copy the dir
3. finished copy redirect page
but noticed if dir is large (ie takes a long time) it is copied but the redirect does not happen, still see "wait" message. If time is few mins then seems to work but once higher eg 15mins (yes big dir) then see the problem.

<?php
set_time_limit(0); <<< otherwise PHP times out script execution
--do the dir copy---
echo "<script type='text/javascript'>
window.location.href='home.php';
</script>";
?>

If you replace the copy with a sleep() you also get a problem ie works for low values eg 5 secs but not higher ones eg 120sec.

Running on sun box with apache - tried increasing httpd "httpd.conf"
Timeout 300
(and restarted httpd) but made no difference
?>

Member Avatar for diafol

how about using a php refresh? Or even a redirect followed by an exit?

header( 'refresh: 5; url=somepage.php' );

I don't know whether this would make a difference. The refresh as I use it just gives my some time to flash a message prior to refresh.

Thanks for the reply but doesnt "header('refresh 5" mean do the redirect after 5 secs? I only want to redirect once the dir copy has completed.

Additional info - I added 2 additions to the top/bottom of my php file:
Top of script : print "start" + time to a log file
Bottom of script : print "end" + time to a log file
And both appear in logfile though browser has not redirected.

Member Avatar for diafol

Yes I believe the refresh does. WHat about a simple php redirect?

header("location: ...");
exit;

"header" cannnot be used as the script has already output a "wait" message.
PHP manual "header": "must be called before any actual output is sent".

Also tried : echo "<meta http-equiv='Refresh' content='0; URL=home.php'>";
but behaviour is the same ie again only works if "delay" is not large

"header" cannnot be used as the script has already output a "wait" message.
PHP manual "header": "must be called before any actual output is sent".

If I recall correctly, using a buffer fixes the problem:

Example:

<?php 

ob_start('ob_gzhandler');
//dircopy
echo "whatever";
header("Refresh: 3; url=somefile.php");
ob_end_flush();

?>

The only problem I can think of is, it'll probably only echo once the dir copy is done :/
I hope this is useful

No ob_start will not work as it supresses the initial warning message telling the user to wait for the cp dir to finish ie script is:-
1. print "wait" message in browser to tell user the cp dir has started
2. do the cp dir
3. redirect page to "finished page" to tell user cp dir has finished

Did you try to do the copy dir through an AJAX call, or is this not something you want ?

Its not just the redirect that fails - it seems javascript fails to work after a lengthy delay - an alert() also fails yet the later php file log write works. So its the browser not responding.

// not working
echo "<script type='text/javascript'>
alert('end');
window.location.href='home.php';
</script>";
// works
$time=date('G:i:s');
fwrite($LOG, "END TIME ===== $time\n");
fclose($LOG);
?>

pritaeas, I am using AJAX to call the php script but I can also put the PHP URL straight into the browser.

I've removed the javascript redirect and just tried using PHP to echo "FINISHED" at the end of the script but this also does not appear in the browser if a large delay occurs, Browser has status of "LOADING". It must be the apache server timing out but I tried increasing timeout (see above) and it made no difference.

Theres no soln to this - browser is timing out waiting for data from the server. I will have to not use the redirect and just stick with printing a message. Thank you for the suggestions.

Member Avatar for diafol

If the execution time is ridiculous, how about breaking the task into bite-size ajax calls with a progress bar? You could have a recursive js function?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.