Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

hide my url

Status
Not open for further replies.
Put your downloads in a directory outside of your webtree. Like if your web files are in /var/ you could put it in /usr/local/sites/downloads or whatever directory you want. Then write a script that checks if a user is logged in, if the user is logged in, fetch the file and print it to the browser.
Such a script could look something like this:
Code:
<?php
$directory = &quot;/path/to/downloads/&quot;; // Remember the trailing slash here
if ($userIsLoggedIn) // Change this line to whatever you use to check if someone is logged in.
{
    $file = preg_replace(array(&quot;/\.\./&quot;, &quot;!/!&quot;), array(&quot;&quot;, &quot;&quot;), $_GET['file']); // Replace .. and /, this secures the script somewhat
    if (file_exists($directory . $file))
    {
        header('Content-type: INSERT MIME TYPE HERE'); // You have to change the mime type here to match the mime type of your documents
        readfile($directory . $file);
    }
    else
    {
        echo &quot;File could not be found.&quot;;
    }
}
else
{
    echo &quot;You have to be logged in to download.&quot;;
}
?>
//Daniel
 
I use these 2 scripts, the first one I embed in the download page, the second you *should* never see and it &quot;pushes&quot; the download at you.

If someone heads directly for download.php it won't be much help to them ;)

*note, change YOUR_HIDDE_DIRECTORY to the right path.

-----------------sender.php
<?php
if(!file_exists(&quot;YOUR_HIDDE_DIRECTORY/$filename&quot;)){
echo &quot;<html><head><title>404 - File not found</title></head<<body>&quot;;
echo &quot;<h1>Unspecified file or file not found</h1><br> File: <b>$filename</b> Not located on this server&quot;;
echo &quot;</body></html>&quot;;
}else{
if(isset($filename)){
$counter = &quot;$filename.txt&quot;;

if(!file_exists($counter)){
echo &quot;File: $filename has been downloaded 0 times<br>&quot;;
}else{

$fd = fopen ($counter, &quot;r&quot;);
$count = fread ($fd, filesize ($counter));
fclose ($fd);

echo &quot;File: <b>$filename </b>has been downloaded <b>$count</b> time(s)<br>&quot;;
}


echo &quot;<form name=download action=download.php method=post>&quot;;
echo &quot;<input type=hidden name=filename value=\&quot;$filename\&quot;>&quot;;
echo &quot;<input type=hidden name=count value=\&quot;$count\&quot;>&quot;;
echo &quot;<input type=hidden name=counter value=\&quot;$counter\&quot;>&quot;;
echo &quot;<input type=submit value=\&quot;download it\&quot;>&quot;;
echo &quot;</form>&quot;;



}else{

echo &quot;<h1> Select a file you muppet!</h1>&quot;;
}
}
?>


------------download.php
<?php
if(isset($filename)){
if(file_exists(&quot;YOUR_HIDDE_DIRECTORY/$filename&quot;)){
$size = filesize('YOUR_HIDDE_DIRECTORY/$filename');
header(&quot;Content-Type: application/save&quot;);
header(&quot;Content-Length: $size&quot;);
header(&quot;Content-Disposition: attachment; filename=$filename&quot;);
header(&quot;Content-Transfer-Encoding: binary&quot;);
$fh = fopen(&quot;YOUR_HIDDE_DIRECTORY/$filename&quot;, &quot;r&quot;);
fpassthru($fh);


// increment counter

$count++;
$fp=fopen(&quot;$counter&quot;,&quot;w&quot;);
fputs($fp, $count);
fclose($fp);
exit;

}else{

echo &quot;No file selected&quot;;

}
}else{
echo &quot;<h1>Use the proper file selection method or go away!</h1>&quot;;
}

?> ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Oh BTW, it uses a text file of $filename.txt to keep track of the number of downloads. ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Wow!, thanks for your reply guys, I'll give them a try :)
 
Im trying what Karver suggested but it gives me an error:

Unspecified file or file not found

I replaced the YOUR_HIDDE_DIRECTORY with the specific path of my setup.exe like this:

if(!file_exists(&quot;
what could be wrong?
 
Ok, I got it working not and its working great. Just another question, how would you make the counter add 1 when the visitor have completely downloaded the file and it doesn't add if the download wasn't complete?
 
oops, i should have said its working great &quot;now&quot; and not &quot;not&quot; hehe...
 
Ohh hard one, you'd need to use java client side to confirm the download back to the server, thats well beyond me I'm afraid.

Another point tho, for more security you can use :
if($HTTP_REFERER == &quot;<download.php code>
}else{
echo &quot;please download the file from my sites links&quot;;
} ______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top