Jump to content

[SOLVED] stop a user typing in a direct path to download a file


jesushax

Recommended Posts

no i dont want to force them to download

 

i want to stop anonymous users from typing in a file path and being able to download a file

if they are a registered users they can click downloads then it will work

 

so if someone typed in the url www.site.com/downloads/videos/thisvideo.mpg it would say not allowed your not a member

 

but if you went to www.site.com logged into members area then clicked downloads > videos > thisvideo.mpg they would e allowed

 

Thanks

no i dont want to force them to download

 

i want to stop anonymous users from typing in a file path and being able to download a file

if they are a registered users they can click downloads then it will work

 

so if someone typed in the url www.site.com/downloads/videos/thisvideo.mpg it would say not allowed your not a member

 

but if you went to www.site.com logged into members area then clicked downloads > videos > thisvideo.mpg they would e allowed

 

Thanks

 

In order to do that the easiest way is to store the files outside of your web root, then use a force download script to allow logged in members to retrieve the files. I don't make suggestions if I don't think there not valid (most of the time anyways).

ive emailed my host, hopeully theyll get a folder set up for me

 

regards to the script for logged in users what would this look like?

 

Cheers

 

That's not something thats going to be covered in a simple forum reply. Should be literally thousands of examples of php login scripts around if you search.

the force downloads script?

 

http://www.phpfreaks.com/forums/index.php/topic,95433.0.html

 

i dont want to force a download

 

i need to know how to reference a folder out of the root directory

 

Cheers

 

You can't, you need to have a script fetch the file for you.

oh right...

 

so i create ahyperlink to the force download page and that will open the file i want it too

 

what do i edit to make it choose the right file, i can see something commented out but i don tknow what that means

 

<?php

// force to download a file
// ex, ( [url=http://localhost/php/download.php?file=C:/Apache]http://localhost/php/download.php?file=C:/Apache[/url] Group/Apache2/hongkong.php )
// hope this can save your time :-)

$file = $_REQUEST['file'];

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".basename($file));

header( "Content-Description: File Transfer");
@readfile($file);

?>

I'll give you an example.

 

download.php

<?php

session_start();

// check user is logged in.
if (!$_SESSION['islogged'])) {
  die("You do not have permission to download files");
}

$file = $_GET['file'];

// the absolute path to your directory storing your files.
$path = '/home/username/files/';

$download = $path.$file;

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download");
header( "Content-Disposition: attachment; filename=".$download);
header( "Content-Description: File Transfer");
@readfile($file);

?>

 

Now, given the address http://yoursite.com/dowload.php?file=foo.jpg your logged in user would get the file located at /home/username/files/foo.jpg. Comparatively, your web root would be within /home/username/htdocs or somewhere similar.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.