Solved

Having trouble using the header() function

Posted on 2003-11-02
3
227 Views
Last Modified: 2008-03-06
I want to setup 2 different pages that will be used for reading and downloading files.   I'm not sure on how to use the header function to differentiate from reading and downloading, or if there is a better way to do this then i'm for that as well.  The files will be a mixture of pdf files and msword files.  
What i'm getting the user to do is pick the file they want to read or download and then open it in the browser for reading if that is what they chose or open up a save box if they want to download.

Hopefully i've explained this well enough
thanks in advance

Adam
0
Comment
Question by:Adam409
  • 2
3 Comments
 
LVL 3

Expert Comment

by:red010knight
ID: 9668699
Have this in its own file in the folder you want to download from.
and the link is:
    /filename.php?f=fileToDL&t=TypeOfFileToDL

<?php
$apptype=$_GET["t"];
$dlFile=$_GET["f"];
header("Content-type: application/".$apptype);
header("Content-Disposition: attachment; filename=".$dlFile);
readfile($dlFile);
?>

Good luck and happy coding - the $_GET might not be right, I haven't had a chance to test the file since I learned of the removal of http_get_vars from php 5.0 so if you are not running that you can put that in and I know it will work.

Enjoy,
Red010Knight
0
 

Author Comment

by:Adam409
ID: 9668836
Thanks for the response Red010Knight


I just had a few more quick question for you if you don't mind... How do you get the filetype of a file again??? and is there any way to execute code after you download the file ?? I've tried to place just print commands afterwards but i get nothing unless the downloading fails.

Much appreciated red010knight
0
 
LVL 3

Accepted Solution

by:
red010knight earned 50 total points
ID: 9669036
the filetype is basically the extension. Sometimes you can get away without it, but I like it just the same because sometimes browsers have conflicts dling files they know how to read.

so for your link if it is a word document the type is doc if it is a adobe acrobat it is pdf and so forth. That should help.

The best way to execute code afterwords is just have a :
header("Location: http://www.example.com/"); //will redirect the browser so that should help

But I haven't tried that before, as the issue never came up so my apologies for not being able to help with that.

and just goto that page...

Sorry I can't be more help in that regard,
Red010Knight
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question