PHP script so that users can download a file from my site without opening a link to a file on my site?

This question says it all. How can I allow users to download a file from my site and not so that they can see which link the file is from? I understand that there may be a need for something like download.php, which will serve as a gateway, but in the past, I do not know what to script next ... If you are tired of writing all the code, several functions are names that I need to use will be very convenient!

+7
scripting file php hide download
source share
4 answers

Find a way to identify the file you are uploading (for example, a GET variable that matches the row identifier in the database or something in the series). Make sure this is true because you do not want your users to be able to download anything from your site. Then use the header with Content-Disposition to tell the browser that the file should be loaded, and readfile to display it.

For example:

 <?php $id = intval($_GET['id']); $query = mysql_query('SELECT file_path FROM files WHERE id = ' . $id); if (($row = mysql_fetch_row($query)) !== false) { header('Content-Disposition: attachment; filename=' . basename($row[0])); readfile($row[0]); } exit; ?> 
+10
source share

You cannot force someone to download a file from a URL without telling them the URL. This is not possible by the HTTP specification. All uploaded ones have a URL.

However, you can have a download URL that works only once, or you need to pass certain information through the POST method. You check the token in the GET or POST variables and this token is invalid once it is used once.

+4
source share

You can use the header() function, which is documented here

I would suggest scrolling down and looking at the 1st example. It seems that you are doing exactly what you want.

+1
source share

readfile should do what you want. Put the actual file outside the web server root and require some credentials before transferring the file.

0
source share

All Articles