473,322 Members | 1,401 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Pass Through Any File?

Hi,

I know php is able to pass a file through, but I
have never done it before.

This is my dilema, I have writen a secure site.
Everyone must login to see the data. But I would
like to secure pictures to start with, but also
other files that people upload. The problem is
that people can upload anything, it's not limited
to to certain file types. Is it possible to create
a php file that will pass any file type through it
from a secured directory outside of the web root
directory. So basicly I can stick the security on
one php file for all the other files that are
uploaded over time. Basicly to stop people from
browsing directly to a file without being logged in.

Hopefully that made sence.
Aug 22 '05 #1
4 9628
*** Smitro wrote/escribió (Mon, 22 Aug 2005 17:11:05 +1000):
to to certain file types. Is it possible to create
a php file that will pass any file type through it
from a secured directory outside of the web root
directory.


Yes.

The functions you need are:

mime_content_type()
header()
readfile()

You have an example at www.php.net/header
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Aug 22 '05 #2
Thanks for the reply.

I tried out this example. (you didn't point to exactly which example
you were refering to)

<?php

function dl_file($file){

//First, see if the file exists
if (!is_file($file)) { die("<b>404 File not found!</b>"); }

//Gather relevent info about file
$len = filesize($file);
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));

//This will set the Content-Type to the appropriate setting for the
file
switch( $file_extension ) {
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpeg":
case "jpg": $ctype="image/jpg"; break;
case "mp3": $ctype="audio/mpeg"; break;
case "wav": $ctype="audio/x-wav"; break;
case "mpeg":
case "mpg":
case "mpe": $ctype="video/mpeg"; break;
case "mov": $ctype="video/quicktime"; break;
case "avi": $ctype="video/x-msvideo"; break;

//The following are for extensions that shouldn't be downloaded
(sensitive stuff, like php files)
case "php":
case "htm":
case "html":
case "txt": die("<b>Cannot be used for ". $file_extension ."
files!</b>"); break;

default: $ctype="application/force-download";
}

//Begin writing headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");

//Use the switch-generated Content-Type
header("Content-Type: $ctype");

//Force the download
$header="Content-Disposition: attachment; filename=".$filename.";";
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len);
@readfile($file);
exit;
}

?>

Fixefox ask if you want to save or open them, but in IE files are just
read straight into the browser (billions of weird characters). I'm
guessing thats because the headers havn't been read properly. I have
tried changeing headers and what not after reading other posts but
can't seem to work it out. Although jpg files work. Interesting though,
I put a PDF file through it and it came out all weird, but when I went
to the properties of the page it said it was an Acrobat PDF file. Any
suggestions on where I've gone wrong?

Aug 23 '05 #3
Unbelievable!

I've been working on this all day, and for some reason it just all of a
suddern worked. No idea how or what... but It worked. I just tried the
above script again in IE and it just worked. No idea what happened.

Aug 23 '05 #4
*** Smitro wrote/escribió (22 Aug 2005 17:07:14 -0700):
I tried out this example. (you didn't point to exactly which example
you were refering to)
There's only one related to this:

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

$file_extension = strtolower(substr(strrchr($filename,"."),1));

//This will set the Content-Type to the appropriate setting for the
file
switch( $file_extension ) {
case "pdf": $ctype="application/pdf"; break;
Apart from the fact that file extension is sometimes unreliable, it's crazy
to hardcode all possible extensions. Why don't you try mime_content_type()?
Fixefox ask if you want to save or open them, but in IE files are just
read straight into the browser (billions of weird characters).


Your code looks fine to me. Unluckily, Internet Explorer is a deprecated
buggy browser that'll force you to many workarounds. Some people recommend
to use a generic content type to force download:

application/octet-stream

Also, check whether you're using sessions, cookies or something else that's
adding headers: IE often gets confused.
--
-- Álvaro G. Vicario - Burgos, Spain
-- http://bits.demogracia.com - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group
--
Aug 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Cory | last post by:
I'm using ASP to run a batch file. I need to know how to pass a variable to this .bat file. Is this possible? I'm using the following code but need to know how to pass ASP variable. set...
5
by: DamonChong | last post by:
Hi, I am still struggling to master C++ and is trying to understand how to achieve passing arguments using pointers. I got some questions that I like to post to the experts here, hope you can...
3
by: Sam | last post by:
Hi, I have a web form I use to allow users upload files to the web server. Instead of handling this operation in the code behind of the web form, I want to create a class that contains document...
2
by: kaushas | last post by:
All I have a file HTML object, I want to pass this object to a class so I can write a common method for uploading files. Idea is to avoid coding file saving in the forms submit button, but call a...
6
by: JoeC | last post by:
I am writing a program and I would like to load a graphic file to a constructor. The graphic stuff shouldn't matter, I want to pass a file name to a constructor and create an object with the data...
5
by: slowmotiongenius | last post by:
All- I have established an adodb recordset in my code-behind, and I need to pass it to the aspx file. I can't seem to figure out if there is a way to do this. I see you can pass a string over...
9
by: One | last post by:
I have a main.php file that calls a php navigation menu. I want to pass the menu file a parameter to tell it which menu to display. Inside the main.php I have : include...
6
by: kath | last post by:
hi everyone......... I have a task, I have fragmented the task into subtask. I have planned to create a class to each subclass and one parent class to handle the sub tasks. Each subclass are...
7
by: Gladen Blackshield | last post by:
Hello All! Still very new to PHP and I was wondering about the easiest and simplest way to go about doing something for a project I am working on. I would simply like advice on what I'm asking...
5
by: Remote_User | last post by:
Hi, Is there any way I can pass a file as a parameter? ( maybe read it into a object) I am working on C# VS2005. Thanks.
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.