9 Replies - 857 Views - Last Post: 13 June 2012 - 12:51 PM Rate Topic: -----

#1 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Question: Force Download Link

Posted 13 June 2012 - 07:19 AM

I am trying to force a download on my web app , and for some reason I can't get it to function properly. When I run my app I see the following f(!SLT_exists($SLT)); { die('Error: File Not Found'); } else { // Set headers header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$SLT.xls"); header("Content-Type: application/zip"); header("Content-Transfer-Encoding: binary"); } // Read the file from disk readfile($SLT);........which is my code. It is not showing an error so I don't know what I am doing wrong. If someone can shed light on this or point me to some documentation I would appreciate it. Here is my code so far...



<?php 
 $SLT = "SLT.xls";		
?>

  if(!SLT_exists($SLT));
 {
   die('Error: File Not Found');
 }
		     
	 else
{
    // Set headers
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=$SLT.xls");
    header("Content-Type: application/zip");
    header("Content-Transfer-Encoding: binary");
}
		     
 // Read the file from disk
    	readfile($SLT);    
	 

This post has been edited by BarNunBoi: 13 June 2012 - 07:20 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Question: Force Download Link

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Question: Force Download Link

Posted 13 June 2012 - 07:35 AM

On line #3 you close the <?php ... ?> block, so all the code that comes after that is, in fact, not read by the PHP interpreter as PHP code. It's just sent through to the browser.
Was This Post Helpful? 1
  • +
  • -

#3 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Question: Force Download Link

Posted 13 June 2012 - 09:50 AM

does any one know of a good link to documentation on forced downloads?
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Question: Force Download Link

Posted 13 June 2012 - 10:22 AM

I don't know of any real documentation, although the web is littered with blog entries and forum posts about it. (Some less incorrect than others...)

But, this is essentially how it works: First of all, you can't really "force" a download. You can only suggest it. It's entirely up to the browser to either download or open the file you are feeding it. However, all the major browsers seem to agree on how to handle the method you're using in your code.

That is: you use the Content-Disposition: attachment header to tell the browser that the file you are sending it is in fact meant to be downloaded, rather than opened. All the other headers you set are either to pass caching info (which is entirely optional), or to pass on common meta-data about the file, like type and size.

The only thing I can see wrong with what you have there -- aside from the error I mentioned earlier -- is that you don't set the Content-Length header. That might be an issue when it comes to file downloads. (Although it really shouldn't prevent it, only cause there to be an "unkown" size in the download dialog.)
header("Content-Length: " . filesize($SLT));



You are also going to want to call exit; right after the readfile($SLT); call, to make absolutely sure nothing that comes after it will corrupt the file data.
Was This Post Helpful? 1
  • +
  • -

#5 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Question: Force Download Link

Posted 13 June 2012 - 10:35 AM

To add to that I believe my content type is wrong. Instead of inputting a xls filetype I typed zip, which doesn't help the cause. With that said, thank you for taking time out to actually explain how the process works!! If I could I would give you 2 thumbs up! Thanks
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Question: Force Download Link

Posted 13 June 2012 - 10:50 AM

If once you've fixed the PHP tags here it compiles without error (and I'm not sure it will), the semi-colon at the end of this line:

if(!SLT_exists($SLT));



means the block immediately after

{
   die('Error: File Not Found');
 }



will ALWAYS execute.
Was This Post Helpful? 0
  • +
  • -

#7 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Question: Force Download Link

Posted 13 June 2012 - 11:39 AM

For some reason I am getting a Parse error: syntax error, unexpected T_ELSE on line 20. With that said, when I click on the button that takes me to the page where the link is supposed to be...the document automatically downloads, and when I click to open the document...the code for that specific page is inside! I don't know how this could've happened. Could you explain what is happening so I can go back and make the needed corrections? Here is my tweaked code

<?php $javascript->link( 'lib/bootstrap-tooltip', false ); ?>
<!-- Right navigation -->
 <?php 
if(Configure::read('show_right_nav.all'))
{echo $this->element('right_navigation'); }
?>

<div class="content" id="container">
	<div class="title"><h5>Add Coverage</h5></div>
	
 	<?PHP
 // Define the path to file
 $file = 'SLT.xls';
 
 if(!file)
 {
     // File doesn't exist, output error
     die('file not found');
 }
 else
 {
     // Set headers
     header("Cache-Control: public");
     header("Content-Description: File Transfer");
     header("Content-Disposition: attachment; filename=$file");
     header("Content-Type: application/xls");
     header("Content-Transfer-Encoding: binary");
    
     // Read the file from disk
     readfile($file);
     exit();
 }
 ?>
 

This post has been edited by BarNunBoi: 13 June 2012 - 11:41 AM

Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6066
  • View blog
  • Posts: 23,526
  • Joined: 23-August 08

Re: Question: Force Download Link

Posted 13 June 2012 - 12:34 PM

$file = 'SLT.xls';
 
 if(!file)


Attention to detail: an absolute requirement.
Was This Post Helpful? 0
  • +
  • -

#9 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Question: Force Download Link

Posted 13 June 2012 - 12:45 PM

I fixed my parse error but for some odd reason coding for my page is showing within the document that I allow users to download.

This post has been edited by BarNunBoi: 13 June 2012 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

#10 BarNunBoi  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 232
  • Joined: 28-March 12

Re: Question: Force Download Link

Posted 13 June 2012 - 12:51 PM

View PostJackOfAllTrades, on 13 June 2012 - 12:34 PM, said:

$file = 'SLT.xls';
 
 if(!file)


Attention to detail: an absolute requirement.


I went back and added a semicolon at the end of line 3. Now Im getting a unexpected t_else error. I have to go back and make some adjustments.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1