logo-dw

Go Back   Dreamweaver Club Forums > Hand Coders Forum > PHP
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Display Modes
Old 11-07-2011, 11:31 PM   #1
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default Force download

I have followed a tutorial to force a download using php.
This:

PHP Code:
<?PHP
 
// Define the path to file
 
$file 'ryboe_tag_cloud.zip';
 
 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/pdf");
     
header("Content-Transfer-Encoding: binary");
    
     
// Read the file from disk
     
readfile($file);
 }
 
?>
is the script that was created.

I used a pdf file, which I know works and have no trouble opening.

When I download it and try to open it I get the message "Not supported file type or the file was sent via email and wasn't decoded properley" or words to that effect.

What have I done wrong?

PS
Im working on a localhost (if that makes any difference)

PPS
I have looked in the properties of the PDF and there seems to be a difference in the sizes of the files

Last edited by gentleone; 11-08-2011 at 09:10 AM.. Reason: posted code wrapped in PHP tags
tony09uk is offline   Reply With Quote
Old 11-08-2011, 09:09 AM   #2
gentleone
gentleone's Avatar
 
Join Date: Nov 2009
Location: Rhodes, Greece
Posts: 2,837
Default

I had to use this too in the past for a client of mine and I had a lot more PHP headers. Below was actually all that I needed for it to work:
PHP Code:
<?php $filename 'documentname.pdf';
  
header("Pragma: public");
  
header("Expires: 0");
  
header("Pragma: no-cache");
  
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
  
header("Content-Type: application/force-download");
  
header("Content-Type: application/octet-stream");
  
header("Content-Type: application/download");
  
header('Content-disposition: attachment; filename=' basename($filename));
  
header("Content-Type: application/pdf");
  
header("Content-Transfer-Encoding: binary");
  
header('Content-Length: ' filesize($filename));
  @
readfile($filename);
  exit(
0);
 
?>
__________________
www.gentlemedia.nl
gentleone is offline   Reply With Quote
Old 11-08-2011, 10:17 AM   #3
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default

Gentle one you are spot on. It works perfectly Thanks.

Now to my next question - in an attempt to learn what is happening - Why wouldn't mine work and why does yours work? Or is this an advanced subject I shouldn't worry about as a beginner?
tony09uk is offline   Reply With Quote
Old 11-08-2011, 11:31 AM   #4
tony09uk
 
Join Date: Oct 2010
Posts: 340
Default

Ive tried to use it to download a jpg now and when I try to preview the jpg I get nothing. When I try to open it in photoshop it works, what can I do to show it in preview/thumbnails in the windows viewer.

I have changed:
header("Content-Type: application/pdf");
to
header("Content-Type: image/jpeg");

I had a quick look at php.net but couldn't seem to find anything that I understood
tony09uk is offline   Reply With Quote
Old 11-08-2011, 12:59 PM   #5
gentleone
gentleone's Avatar
 
Join Date: Nov 2009
Location: Rhodes, Greece
Posts: 2,837
Default

I have no clue about both questions... back then this was soley a copy/paste action of mine... yes, I was guilty... I know

Some of the PHP gurus here might help you out with that
__________________
www.gentlemedia.nl
gentleone is offline   Reply With Quote
Old 11-09-2011, 02:10 AM   #6
edbr
edbr's Avatar
 
Join Date: Aug 2005
Location: Bali
Posts: 11,162
Default

http://elouai.com/force-download.php. Yahoogle
__________________
If you're happy and you know it shake your meds!
different style links examples

Flight / Hotel search
Free script download
Bali Villas
edbr is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 09:48 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Copyright 2006 DreamweaverClub.com