How to protect file url with PHP?
Collapse
X
-
@ 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....
-
Originally posted by GumSlone View Postyou will also need to send correct headers.PHP Code:$file_url = 'http://some_site.com/pathe/file.jpg';
$contents = file_get_contents($file_url);
echo $contents;
if the file is located on your site i would recommend to useor you can also use rewrite rules in you htaccessPHP Code:echo readfile($file_path);
Something else to mention :p don't use $_GET['file']; in afunction because someone else can download your site files // via modified url ..PHP Code:echo readfile($file_path);
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:
you could select the path from an database ..PHP Code:<?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;
}
?>
example:
$file =mysql_query("SELECT * FROM table WHERE fileid='".$myid"'.");
readfile($file['path']);Last edited by just_m3.; 10.01.12, 11:15.
Leave a comment:
-
gumslone right you can protect your files using fread with correct header type to make it downloadable
Leave a comment:
-
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....
Leave a comment:
-
Originally posted by yoblooc295 View Postcan 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
you will also need to send correct headers.PHP Code:$file_url = 'http://some_site.com/pathe/file.jpg';
$contents = file_get_contents($file_url);
echo $contents;
if the file is located on your site i would recommend to useor you can also use rewrite rules in you htaccessPHP Code:echo readfile($file_path);
Leave a comment:
-
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.
Leave a comment:
-
How to protect file url with PHP?
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 goodTags: None
Leave a comment: