473,216 Members | 2,240 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,216 software developers and data experts.

Programmatically "deliver" file

I would like to make my downloads section unbrowsable (to users) but
accessible to scripts.

Can I deliver a file to a browser without linking to it's URL so that I
can deliver files programmatically but prevent users from browsing or
linking to them?

I am using PHP on an apache server.

Ta

Jul 17 '05 #1
10 4713
NC
St***********@gmail.com wrote:

I would like to make my downloads section unbrowsable (to users)
but accessible to scripts.


There are two ways to implement this:

1. Place the downloads directory outside the document root.

Not all hosts allow this, but those that do typically have
an initial setup like this:

\ [Root directory]
\public_html

When a client requests, say, www.yoursite.com/index.php,
the index.php is served from the public_html directory.
The root directory remains inaccessible via HTTP. So
what you can do is to place your downloads directory
outside \public_html:

\ [Root directory]
\public_html
\downloads

Then your download script could do something like this:

header('Content-Type: application/zip');
readfile('/downloads/somefile.zip');

2. Protect the downloads directory by using an .htaccess file.

Your downloads directory can be located inside the
document root, but you can disable HTTP access to it
by putting an .htaccess file in that directory containing
the following directives:

Order allow,deny
Deny from all

Then your download script could do something like this:

header('Content-Type: application/zip');
readfile('downloads/somefile.zip');

Cheers,
NC

Jul 17 '05 #2
St***********@gmail.com says...
I would like to make my downloads section unbrowsable (to users) but
accessible to scripts.

Can I deliver a file to a browser without linking to it's URL so that I
can deliver files programmatically but prevent users from browsing or
linking to them?


Absolutely yes.

Put the files in a directory which is outside the Apache document root
(which means that users can never web browse directly to them). PHP can
access files anywhere on the file system (that it is allowed to), not just
the directories within Apache's doc root.

Quick and dirty /somewhere/inside/docroot/download.php script:

<?php
$filepath='/somewhere/outside/docroot/';
$filename='somedownload.zip';
header('Content-Disposition:attachment; filename="'.$filename.'"');
header('Content-type:application/octet-stream');
header('Pragma:no-cache');
header('Expires:0');
if ($file=fopen($filepath.$filename,'r')) {
fpassthru($file);
fclose($file);
}
?>

Geoff M

Jul 17 '05 #3
Hi - many thanks for the reply.

I tried this and it asks me to open or save (as usual) but the document
it opens is the php file with the content of the attachment within it.
So the downloaded file is called deliverfile.php and is full of random
characters and the text of the attachment I wanted to download...

Jul 17 '05 #4
Thanks very much - this works a treat.

Jul 17 '05 #5
Eeek!!! Actually it doesn't work a treat.

Although it finds the file and downloads it - when it tries to open it
in the relevant application it doesn't work. WinZip says the file is
corrupted. Word says it can't be opened. Etc.

Help!!!

Many thanks

Jul 17 '05 #6
St***********@gmail.com says...
I tried this and it asks me to open or save (as usual) but the document
it opens is the php file with the content of the attachment within it.
So the downloaded file is called deliverfile.php and is full of random
characters and the text of the attachment I wanted to download...


May be a known problem with your browser (suspect a particular version of
MSIE) not correctly accepting the header directive for the filename. Try:

a) using another browser (Firefox?) and see if the problem still exists
b) downloading the file, renaming it see if the file content is OK.

If BOTH these methods work, then just put a warning message on you page
about the possible requirement for the user to rename the file.

Geoff M
Jul 17 '05 #7
St***********@gmail.com says...
Eeek!!! Actually it doesn't work a treat.

Help!!!


Steve,

Many of us use a newsgroup client application rather than Google groups
and will have difficulty figuring out to which response you are replying.
Please quote a (minimal) amount of the message to which your are replying.

GM
Jul 17 '05 #8
> May be a known problem with your browser (suspect a particular
version of
MSIE) not correctly accepting the header directive for the filename. Try:
a) using another browser (Firefox?) and see if the problem still exists b) downloading the file, renaming it see if the file content is OK.

All the files work fine if I just link to them so the content is fine.
The files also download programmatically in firefox - so I guess it is
the IE bug.

Many thanks for your help.

Steve

Jul 17 '05 #9
> All the files work fine if I just link to them so the content is
fine.
The files also download programmatically in firefox - so I guess it is the IE bug.


Just for info, I ended up moving the downloads folder into the root
folder (outside of the public_html folder) and then whenever a request
is made for a document, I run a script that copies the file into a
separate folder within the public_html and then link to it.

A scheduled clean up script deletes the file.

Bit long winded, I know. But it works.

Thanks for all your help guys.

:o)

Jul 17 '05 #10
On 2005-03-31 15:59:36 +0100, St***********@gmail.com said:
I would like to make my downloads section unbrowsable (to users) but
accessible to scripts.

Can I deliver a file to a browser without linking to it's URL so that I
can deliver files programmatically but prevent users from browsing or
linking to them?

I am using PHP on an apache server.

Ta
This is what I use on a PDF library I have, all you'd need to do is add
some logic in for the content-type and you're good to go.

Andrew

$filename =
'/home/user/a_path_not_available_via_web/documents/documentname.pdf';

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/pdf");
header("Content-Disposition: attachment;
filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile($filename);
exit();

Feb 23 '07 #11

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

Similar topics

13
by: Robert Zierhofer | last post by:
Hi all, I currently face a problem with htmlentities and german "umlaute". After moving my scripts to a new box (from Linux to FreeBSD) I had to see that htmlentities is not working anymore....
2
by: \Dandy\ Randy | last post by:
Hello everyone. I have been following misc posts, as well as reading several FAQ's on this issue, unfortunatley I cannot locate a solution. I am hoping that someone will be able to provide me with...
7
by: Sherry Littletree | last post by:
Hi All I am working on a site that has a large amount of common html on all its web pages. I am looking for a way to place this in a single file so, if changes are made, I can change this...
99
by: Jim Hubbard | last post by:
It seems that Microsoft not only does not need the classic Visual Basic developer army (the largest army of developers the world has ever seen), but now they don't need ANY Windows developer at a...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
4
by: mattsthompson | last post by:
Im writing a DLL that extends IHttpHandler to intercept requests for a certain file extension and deliver watermarked images. I'm using LeadTools' .NET framework for the image manipulation and it...
9
by: Anubhav Jain | last post by:
Hi, I am having few .net source files(.cs or .vb) and I want to dynamically generate the corresponding .net project file(.csproj or .vbproj) for them without using visual studio.So that I could...
7
by: Alex Maghen | last post by:
I have a DataGrid control with a LinkButton command column that deletes the row. What I want to do is set it up so that there's a client-side Confirm alert BEFORE the actual Delete command gets...
3
by: sophie_newbie | last post by:
Hi, I want to store python text strings that characters like "é" "Č" in a mysql varchar text field. Now my problem is that mysql does not seem to accept these characters. I'm wondering if there...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.