?
Solved

Having trouble using the header() function

Posted on 2003-11-02
3
Medium Priority
?
260 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 150 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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

839 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