Solved

Having trouble using the header() function

Posted on 2003-11-02
3
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

717 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