Auto close window after redirecting to file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Auto close window after redirecting to file

Post by Chris Corbyn »

Hi,

I have a page which opens (download.php) when user clicks submit on a form. This page is set to redirect to a file and force a download if the values on the form were correct. I've done this using the header function to redirect and force download (rather than opening).

The only problem is that the window remains open even once the download dialog box has appeared and I would like it to close itslef after running the script

Code: Select all

header('Content-type: Audio/Midi');
		header('Content-Disposition: attachment; filename="Abs - Miss perfect.mid"');
		readfile('Abs - Miss perfect.mid');
I've tried using <script>Window.close()</script>
but it didn't do anything.

I also tried
exit;
after my header functions but it still didn't close the window.

Any idea's how to do it?
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

>> I've tried using <script>Window.close()</script>

Javascript is case-sensitive, try window.close() (lowercase w)
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

The script language needs to be defined...

Code: Select all

<script language="JavaScript">
window.self.close();
</script>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Tried both considerations. Still no luck. Am I right to do it like this?...

Code: Select all

<?php

$tone=$_POST['file'];
if (empty($tone)) {
$tone='index';
}

/*Check that authorisation code matches filename of image*/

if (strpos($_POST['auth'], substr($_POST['code'], 0, -4)) !== false) {
                switch ($tone) {
	
                case 'noka_1';

/*Redirection to file using header function and force download*/

		header('Content-type: Audio/Midi');
		header('Content-Disposition: attachment; filename="Abs - Miss perfect.mid"');
		readfile('Abs - Miss perfect.mid');
	break;
                exit;
echo '<script language="JavaScript">';
echo 'window.self.close();';
echo '</script>';
                }
}
else {
echo 'Sorry wrong code, please close this windows and try again';
}

?>
Thanks again
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

You have a semi-colon on the "case" line, should be

Code: Select all

<?php

//code...

    switch($tone){
    case 'noka_1':
        //do something

?>
But in this case, you defined $tone as 'index', so the above statement will never be true.

-edit-

One more thing, you have both a break; and and exit; in there. You want the javascript code to be executed after the header stuff, right? exit; causes the compiler or whatever it is to stop executing all code after the exit command.

-edit-
Oops.. didn't see your post there Benty... 8O
Last edited by Steveo31 on Fri Mar 26, 2004 3:06 pm, edited 2 times in total.
TheBentinel.com
Forum Contributor
Posts: 282
Joined: Wed Mar 10, 2004 1:52 pm
Location: Columbus, Ohio

Post by TheBentinel.com »

d11wtq wrote:

Code: Select all

readfile('Abs - Miss perfect.mid');
	break;
                exit;
echo '<script language="JavaScript">';
echo 'window.self.close();';
echo '</script>';
Once the exit; is hit, nothing after it is sent to the browser. However, even if you take the exit out, I don't think it's going to work. (Though it's worth trying) I think the <script... text will just get appended onto the .mid file.

Hmmm... not sure how to do this. Please try taking out the exit and let us know if it works.

EDIT: Oh yeah, and move the break down.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

I've tried it without the exit; too and still had no luck.

Also, I've set $tone as whatever $_POST['file'] info is sent from the form on the last page so $tone=noka_1 comes from url.php?file=noka_1 (it has been passed around a few pages by the time it reaches download.php so the name changed somewhere along the way and it ended up as $tone so it does work. If I wanted to I could add case 'index': to the switch (in fact i did that within the last five mins just to put a prompt that a file must first be selected before submitting.

Basically, everything on that script works the way I'd like it to apart from my neccessity to close the window after redirection.

Thanks for trying anyway. :-)
Steveo31
Forum Contributor
Posts: 416
Joined: Sun Nov 23, 2003 9:05 pm
Location: San Jose CA

Post by Steveo31 »

Try this:

Code: Select all

<?php 

$tone=$_POST['file']; 
if (empty($tone)) { 
$tone='index'; 
} 

/*Check that authorisation code matches filename of image*/ 

if (strpos($_POST['auth'], substr($_POST['code'], 0, -4)) !== false) { 
                switch ($tone) { 
    
                case 'noka_1'; 

/*Redirection to file using header function and force download*/ 

      header('Content-type: Audio/Midi'); 
      header('Content-Disposition: attachment; filename="Abs - Miss perfect.mid"'); 
      readfile('Abs - Miss perfect.mid'); 
?>
<script language="JavaScript"> 
window.self.close();
</script>
<?php
break; 
                } 
} 
else { 
echo 'Sorry wrong code, please close this windows and try again'; 
} 

?>
Not sure what changes you made, but my point is stop the PHP and just do regular HTML syntax, then reopen the PHP.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

<?php
$tone=$_POST['file']; 
if (empty($tone)) { 
$tone='index'; 
} 

/*Check that authorisation code matches filename of image*/ 

if (strpos($_POST['auth'], substr($_POST['code'], 0, -4)) !== false) { 
                switch ($tone) { 
    
                case 'noka_1'; 

/*Redirection to file using header function and force download*/ 

      header('Content-type: Audio/Midi'); 
      header('Content-Disposition: attachment; filename="Abs - Miss perfect.mid"'); 
      readfile('Abs - Miss perfect.mid'); 
?> 
<script language="JavaScript"> 
window.self.close(); 
</script> 
<?php 
break; 
                } 
} 
else { 
echo 'Sorry wrong code, please close this windows and try again'; 
} 
?>
Still doesn't work I'm afraid.
User avatar
bapper
Forum Newbie
Posts: 5
Joined: Thu Oct 21, 2004 1:49 am

Post by bapper »

How did it go on this issue?
I've got the same problem but no solution.
The download part works fine but when it comes to handle activities after it does not work at all.
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

Something i used, and works just fine

Code: Select all

<script language="JavaScript">
var gWindowCloseWait = 1;

function SetupWindowClose()
&#123;
	window.setTimeout("window.close()",gWindowCloseWait*1000);
&#125;

var gSafeOnload = new Array();
function SafeAddOnload(f)
&#123;
	isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
	IEmac = ((document.all)&&(isMac)) ? true : false;
	IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
	if (IEmac && IE4)  // IE 4.5 blows out on testing window.onload
	&#123;
		window.onload = SafeOnload;
		gSafeOnload&#1111;gSafeOnload.length] = f;
	&#125;
	else if  (window.onload)
	&#123;
		if (window.onload != SafeOnload)
		&#123;
			gSafeOnload&#1111;0] = window.onload;
			window.onload = SafeOnload;
		&#125;		
		gSafeOnload&#1111;gSafeOnload.length] = f;
	&#125;
	else
		window.onload = f;
&#125;
function SafeOnload()
&#123;
	for (var i=0;i<gSafeOnload.length;i++)
		gSafeOnload&#1111;i]();
&#125;
SafeAddOnload(SetupWindowClose);
</script>
User avatar
bapper
Forum Newbie
Posts: 5
Joined: Thu Oct 21, 2004 1:49 am

Post by bapper »

:roll: Well... that might work but it would still be a work around to the actual problem.

I invoke download.php via <a href=''>, it opens the dialogue box with the possibility to open, save or cancel. So far so good.
After I've saved I start to get problems with the javascript:
On the same page I've got a script switching between two images upon onClick. Now, after the download part I get "Unspecified error", in IE 6.0.2800, when I click on the picture.
So I start bug testing the javascript:

Code: Select all

function switchimage(imgname)&#123;
 obj = document.getElementById(imgname)
  if(obj.nameProp != imgname+"_mission.gif")&#123;
       obj.src = "../../images/forum/"+ imgname +"_mission.gif"
  &#125;else&#123;
       obj.src = "../../images/forum/"+ imgname +".gif"
  &#125;
&#125;
obj = document.getElementById(imgname) is causing the problem. I can alert(imgname) but that's about it.

Can it be since I've told IE to send binary information?

Download.php looks like this:

Code: Select all

<?php
if(!isset($file)) $file = null;
@$file = $_REQUEST['file'];
if($file) 
{ 
	if(file_exists($file)){
   	header('Content-Disposition: attachment; filename="'.basename($file).'"'); 
		header('Content-Transfer-Encoding: Binary');
		header('Content-type: application/force-download');
   	header("Content-Length: ".filesize($file));
		@readfile($file);
		exit();
	}
}
?>
What I did to solve the problem, it's a work around:
<iframe name='idownload' width='0' height='0'></iframe>
<a href='download.php?file=blabla.pdf' target='idownload'>download</a>

This solution works but I still don't understand why the script that works fine before the download suddenly halts. Does anybody have a clue?


feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

trying to output any sort of "text" after a readfile call will be stuck inside that stream. You are sending a download with this script.. then that's all you can do with it. The page request calling this code (through the client) can ask the window to close itself.
User avatar
bapper
Forum Newbie
Posts: 5
Joined: Thu Oct 21, 2004 1:49 am

Post by bapper »

trying to output any sort of "text" after a readfile call will be stuck inside that stream. You are sending a download with this script..
Yes, thanks. I've realised that. Do you have an alternative solution?
then that's all you can do with it.
Ok, so there's really no way to run a javascript after a download?
The page request calling this code (through the client) can ask the window to close itself.
Ok, how?
Post Reply