Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-25-2012, 01:11 PM   PM User | #1
dhakad_ram
New to the CF scene

 
Join Date: Sep 2012
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
dhakad_ram is an unknown quantity at this point
Smile restrict direct download of a file

Can i restrict a file to direct download.suppose in my domain i am having a upload folder in which i am having a exam.pdf file then it should not be downloaded by accessing url http://www.mydomain.com/upload/exam.pdf.If it is possible then please tell me the way.
dhakad_ram is offline   Reply With Quote
Old 09-25-2012, 10:08 PM   PM User | #2
kbluhm
Senior Coder

 
kbluhm's Avatar
 
Join Date: Apr 2007
Location: Philadelphia, PA, USA
Posts: 1,502
Thanks: 2
Thanked 258 Times in 254 Posts
kbluhm will become famous soon enough
Place the file in a non-public folder and use readfile() to send the contents to the client, as well as header() to set the file's name, type, size, and so on.

Quick example, untested... to download exam.pdf you'd load files.php?id=1
PHP Code:
<?php # files.php

/* - - - - - - - - - -
 * Our hidden files
 */

$files = array(
    
=> 'exam.pdf',
    
=> 'exam2.pdf',
    
=> 'exam3.pdf',
);

/* - - - - - - - - - -
 * Ensure `?id=xxx` is set and valid
 */

if ( ! isset( $_GET['id'] ) )
{
    
header'Location: /' );
    exit;
}

$id = ( int ) $_GET['id'];

if ( ! isset( 
$files$id ] ) )
{
    
header'Location: /' );
    exit;
}

/* - - - - - - - - - -
 * Full path to the file
 */

$path '/var/non-www/files/' $files$id ];

/* - - - - - - - - - -
 * Ensure $path is a valid file
 */

if ( ! is_file$path ) )
{
    
header'Location: /' );
    exit;
}

/* - - - - - - - - - -
 * Send the file as a download with proper headers
 */

header'Content-type: application/octet-stream' );
header'Content-Disposition: attachment; filename="' basename$path ) . '"' );
header'Content-length: ' filesize$path ) );
readfile$path );
__________________
ZCE

Last edited by kbluhm; 09-26-2012 at 04:41 AM..
kbluhm is offline   Reply With Quote
Old 09-25-2012, 10:53 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,505
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Using readfile() can of course hit the scripts maximum execution time limit.

If your server has it enabled, you would be wise to set_time_limit(0)
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Reply

Bookmarks

Tags
restrict direct download

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:42 PM.


Advertisement
Log in to turn off these ads.