Display file if possible, otherwise download

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Display file if possible, otherwise download

Post by shiznatix »

I am currently making a media holder website. On this site, there will be lots of types of files, such as .swf, images, etc. I have the files being uploaded to a directory that is not in the server root so that people can not directly access the files, you have to download them from the site.

I also want to have a preview option though. I would like to be able to display a flash banner without using the html markup, just so people can see what it is.

Problem though is that the files are not in the web root. Here is the folder structure:
files/
public_html/

So, how can I read a .swf file (or .jpg) to the browser without it accessing it directly?
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Display file if possible, otherwise download

Post by Darhazer »

readfile
Before that you have to output the correct Content-type and Content-Length headers. The second one is easy - use the filesize() function
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Display file if possible, otherwise download

Post by shiznatix »

Thanks, that seams to have done the trick!
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Re: Display file if possible, otherwise download

Post by shiznatix »

On second thought, is there a list of all file extensions and their ctype? I have a bunch of different file types and I want to be able to set the correct header every time
Post Reply