473,407 Members | 2,326 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,407 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 9632
*** 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.