473,626 Members | 3,427 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Programmaticall y "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 programmaticall y but prevent users from browsing or
linking to them?

I am using PHP on an apache server.

Ta

Jul 17 '05 #1
10 4746
NC
St***********@g mail.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('downl oads/somefile.zip');

Cheers,
NC

Jul 17 '05 #2
St***********@g mail.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 programmaticall y 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='some download.zip';
header('Content-Disposition:att achment; filename="'.$fi lename.'"');
header('Content-type:applicatio n/octet-stream');
header('Pragma: no-cache');
header('Expires :0');
if ($file=fopen($f ilepath.$filena me,'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***********@g mail.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***********@g mail.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 programmaticall y 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 programmaticall y 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

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

Similar topics

13
8518
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. The BSD Server (FreeBSD 5.1.2) runs PHP 4.3.9 and Apache 2 as well as the Linux Server does/did too. I also tried defining the charset with ISO 8859-1 as 3rd parameter in htmlentities but without a result.
2
4253
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 the simple answer. My problem has to do with the leading white spaces after the first line when calling data using the @ variable. Here is my code: open (PREVIEW, "<preview.txt") or &error("Unable to open the data file for reading"); flock...
7
5064
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 single file and do not have to change each and every page. I have the Java scripting in a common .Js file but have not been able to find a way to do this with my html content.
99
6071
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 small or mid-sized business. http://groups-beta.google.com/group/microsoft.public.msdn.general/browse_thread/thread/9d7e8f9a00c1c7da/459ca99eb0e7c328?q=%22Proposed+MSDN+subscription+changes%22&rnum=1#459ca99eb0e7c328 Damn! To be that...
81
7289
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 be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
4
1969
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 is their Codecs.Load which issues the error. I have the code working when the image is loaded from my local machine but when loading from a network share I receive a File Not Found error from the framework that is attempting to load then file...
9
5435
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 be able to generate and compile the project on the enviroments where Visual Studio.Net is not installed. Thanks and Regards, Anubhav Jain MTS Persistent Systems Pvt. Ltd. Ph:+91 712 2226900(Off) Extn: 2431 Mob : 094231 07471
7
5262
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 called on the server-side. That's easy to do with normal buttons, etc. as follows... <asp:Button ID="ConfirmBtn" Text="ConfimMe!" OnClientClick="if(!confirm('Are you sure?'))return false;" OnClick="ConfirmBtnClickHandler" Runat="server" /> But...
3
2266
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 is any way I can somehow "encode" these characters to appear as normal characters and then "decode" them when I want to get them out of the database again? -Thanks.
0
8272
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8205
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8644
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8370
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8514
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7206
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6126
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4208
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1516
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.