Solved

Having trouble using the header() function

Posted on 2003-11-02
3
210 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now