How to protect file url with PHP?

Started on the old forum in December 2011 · view the original in the archive · old links redirect here

6 replies · 1,065 views · started 14 years ago · 1 min read

6 participants
yoblooc295 Newbie · 8 posts #1

can anyone help me? i want to protect my file's url with PHP but i don't know how to do that can u share php code to do that? i'm vietnamese,so my english not good

Pray for world

khan Lead · 681 posts #2

can you be more specific with example? its too complicated to understand. do u want to protect ur files from direct download? or u wanna hide ur files url with another.. in first case you can use hotlink protection and in second case you can use mod_rewrite on .htaccess.

GumSlone Administrator Principal · 1,690 posts #3

yoblooc295 wrote: can anyone help me?

i want to protect my file's url with PHP but i don't know how to do that can u share php code to do that? i'm vietnamese,so my english not good

$file_url = 'http://some_site.com/pathe/file.jpg';
$contents = file_get_contents($file_url);
echo $contents;

you will also need to send correct headers.

if the file is located on your site i would recommend to use

echo readfile($file_path);

or you can also use rewrite rules in you htaccess

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

razzbee Developer · 130 posts #4

Gumslone's question is right... the best way is to put the file out side the folder where it can be executed eg. public_html ,using the correct headers,use echo readfile($file_path); as Gumslone said....

timo Junior · 30 posts #5

gumslone right you can protect your files using fread with correct header type to make it downloadable

just_m3. Developer · 199 posts #6

GumSlone wrote:

$file_url = 'http://some_site.com/pathe/file.jpg';
$contents = file_get_contents($file_url);
echo $contents;

you will also need to send correct headers.

if the file is located on your site i would recommend to use

echo readfile($file_path);

or you can also use rewrite rules in you htaccess

Something else to mention 😛 don't use $_GET['file']; in a

echo readfile($file_path);

function because someone else can download your site files // via modified url ..

for example if someone requests connectdb.php via your link like download.php?file=connectdb.php it will download your file .. now depends on the header you use to save the files ..

ex:

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

you could select the path from an database .. example:

$file =mysql_query("SELECT * FROM table WHERE fileid='".$myid"'.");

readfile($file['path']);

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

razzbee Developer · 130 posts #7

@ just_m3 its true,again if possible,U can check the ip for double request to prevent data copiers and bot copiers from wasting ur bandwith for their benefits....

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close