Hello
I make a website for downloading different programs, download links are temporary (approx. Http://site.ru/downloads/bdb33c054e7f120b74f7867efcfa0633).

When you click on this link, the script sends the file through the headers and does not directly follow the link (this is if the file exists).

And how to make it so that in the absence of a file (the one that is given through headers) the link to the link is not carried out?

Thanks in advance!

The code on the page. $ file - the file address.

if (file_exists($file)) { // сбрасываем буфер вывода PHP, чтобы избежать переполнения памяти выделенной под скрипт // если этого не сделать файл будет читаться в память полностью! if (ob_get_level()) { ob_end_clean(); } // заставляем браузер показать окно сохранения файла header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . rand(0,999).'.'.end(explode(".", basename($file))) ); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); // читаем файл и отправляем его пользователю readfile($file); exit; } 
  • Give a code for a thread, but it is not clear what you are trying to do. - zb '
  • 2
    And what prevents to check if there is a file on the previous page and not to generate a link? Well, if only not protection from direct links of course. But this is just a refinement. Well, as for the "not transition", there is no way here, because HTTP anyway gets the GET URL on click, and something still needs to be given back to the client - either the content (page) or send it to the standard 404. - void
  • So satisfied? <a href="address_na_file" target="x"> test </a> <iframe name = "x" style = "display: none"> </ iframe> - ReinRaus

1 answer 1

By clicking or nothing will happen if you return 404

 if (file_exists($file)) { // если ob_start() вызван более одного раза while(ob_get_level()) ob_end_clean(); header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . rand(0,999).'.'.end(explode(".", basename($file))) ); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . filesize($file)); // читаем файл и отправляем его пользователю readfile($file); }else{ // отдаём 404, что важно для поисковиков header('Content-Type: application/octet-stream'); header('HTTP/1.1 404 Not Found'); header('Status: 404 Not Found'); } 
  • Chrome follows the link and displays: Error. The link does not work. Try it: Search Google: - ReinRaus
  • @ReinRaus, you are right. I think our advice should be combined with my answer - Gedweb
  • The reaction of chromium is his own business :) It is important that the link is given, it is checked simply: wget -S " link ". If there is an honest 404 without frills, then everything is in order, if something else - you need to understand. Well or not the fact that in general it is necessary to give 404, who prevents to give 200 and a page with explanations? Moreover, search engines do not need these links. - user6550 February
  • @klopp, if there is a pure 404, then the browser should show a 404 page. Chrome is the first browser on which the solution did not work, the rest did not test. Soon and in the opera will also work when she goes to the webkit - ReinRaus
  • 2
    @klopp, 200 absolutely not. From personal experience, there are a lot of dead links on the search engine site, which is not very pleasing to users. You need to work correctly with HTTP - Gedweb