?
Solved

Having trouble using the header() function

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

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

770 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