saco721 Posted September 23, 2008 Share Posted September 23, 2008 Hi, I have a problem with php headers. Basically I have a script that opens a saveas dialog box for downloading files. It works fine. The problem is that when I add an addional header to open a new page after the download is complete, the dialog box stops appearing and it just loads the page. Here is the code: <?PHP header("Content-type: application/octet-stream"); header("Content-Length: ".filesize($filename)); header("Content-Disposition: attachment; filename=$filename"); header('Location: http://www.testdl.com');//dialog does not popup but loads new page when this header is added $fp = fopen($filename, 'rb'); fpassthru($fp); fclose($fp); ?> Where am I going wrong? Any help would be greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/125464-problem-with-php-header/ Share on other sites More sharing options...
jonsjava Posted September 23, 2008 Share Posted September 23, 2008 header redirect can only happen when you have not output anything to the screen. If you have sent anything out, then it just won't work. Link to comment https://forums.phpfreaks.com/topic/125464-problem-with-php-header/#findComment-648601 Share on other sites More sharing options...
saco721 Posted September 23, 2008 Author Share Posted September 23, 2008 thanks for the reply. Are there any other alternatives I can use? Link to comment https://forums.phpfreaks.com/topic/125464-problem-with-php-header/#findComment-648605 Share on other sites More sharing options...
redarrow Posted September 23, 2008 Share Posted September 23, 2008 Here's the code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Your Page Title</title> <meta http-equiv="REFRESH" content="0;url=http://www.the-domain-you-want-to-redirect-to.com"></HEAD> <BODY> Optional page text here. </BODY> </HTML> [/cdoe] Link to comment https://forums.phpfreaks.com/topic/125464-problem-with-php-header/#findComment-648609 Share on other sites More sharing options...
saco721 Posted September 23, 2008 Author Share Posted September 23, 2008 Hi, I have tried the code you provided me with and it does load the new page but doesnt open the saveas dialog. Have I done it right? here is the code: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Your Page Title</title> <meta http-equiv="REFRESH" content="0;url=http://www.redirect.com"></HEAD> <BODY> </BODY> </HTML> <?PHP header("Content-type: application/octet-stream"); header("Content-Length: ".filesize($filename)); header("Content-Disposition: attachment; filename=$filename"); $fp = fopen($filename, 'rb'); fpassthru($fp); fclose($fp); ?> thanks for your time! Link to comment https://forums.phpfreaks.com/topic/125464-problem-with-php-header/#findComment-648621 Share on other sites More sharing options...
jonsjava Posted September 23, 2008 Share Posted September 23, 2008 try putting the HTML in after the PHP header content. Link to comment https://forums.phpfreaks.com/topic/125464-problem-with-php-header/#findComment-648656 Share on other sites More sharing options...
discomatt Posted September 23, 2008 Share Posted September 23, 2008 This won't work.... you can't echo HTML when you've declared the script as a stream and expect it to be parsed You have to call the PHP script serving the file in a new window or a frame, and then redirect in the parent page. Link to comment https://forums.phpfreaks.com/topic/125464-problem-with-php-header/#findComment-648666 Share on other sites More sharing options...
saco721 Posted September 26, 2008 Author Share Posted September 26, 2008 Hi!, im still having problems getting the page to refresh after saveas dialog is called. Any help would be greatly appreciated, code would be even better thanks. Link to comment https://forums.phpfreaks.com/topic/125464-problem-with-php-header/#findComment-651252 Share on other sites More sharing options...
discomatt Posted September 26, 2008 Share Posted September 26, 2008 You have to create an IFRAME in your HTML that links to the download script. You can then reload/redirect in the parent HTML page. Link to comment https://forums.phpfreaks.com/topic/125464-problem-with-php-header/#findComment-651265 Share on other sites More sharing options...
saco721 Posted September 26, 2008 Author Share Posted September 26, 2008 Hi!, thanks for the advice. I have created a page using an iframe : <html> <body> <iframe src="filedownload.html" width="40%" height="80" align="right" border = "1"> </iframe> </body> </html> filedownload is : <html> <head> </head> <body> <a href="filedownload4.php?filename=filetodownload.txt">Download this text file</a> </body> </html> filedownload4.php is : <?PHP header("Content-type: application/octet-stream"); header("Content-Length: ".filesize($filename)); header("Content-Disposition: attachment; filename=$filename"); $fp = fopen($filename, 'rb'); fpassthru($fp); fclose($fp); ?> what is the code to refresh the page and where does it go? Sorry but I am new to php. Thanks very much! Link to comment https://forums.phpfreaks.com/topic/125464-problem-with-php-header/#findComment-651273 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.