I have some process in page. It’s downloading a file.
<?php
// getting file with CURL
$ch = curl_init ('http://adress.com/file.csv');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,7500);
$rawdata=curl_exec($ch);
curl_close ($ch);
// End Curl Process
// SAVE FILE
$name = Rand(100000,1000000);
$fp = fopen('files/'.$name.'.csv','w');
fwrite($fp, $rawdata);
fclose($fp);
// END SAVE PROCESS
// REDIRECT OTHER PAGE
**header('Location: x.php');**
// END OF PAGE
?>
This is allright. But there is a problem. It redirects without complete process. I want to redirect after all processes are done. The file is empty when i redirect with that method.
What must I do? How can i redirect page after all processes are done? php or javascript/jquery.
Your code should be fine, and should complete the process before redirecting.
PHP runs in a single thread, so the next statement won’t be executed until the previous one completes. This means that your
header()line won’t execute untilfwrite()has finished writing the file.In fact, setting a
Locationheader doesn’t even mean the PHP script stops. You can test this by creating a dummy file and runningunlink()on it after issuing theLocationheader. The file will still be deleted. All thatLocationdoes is sends a new location to your browser. Your browser then retrieves the new page before the old one is done running.