0

I upload some mp4 files to my private folder and some users can access those files. this is my code for letting them download my mp4 files:

function my_module_file_download($uri) {

  // Get the file record based on the URI. If not in the database just return.
  $files = file_load_multiple(array(), array('uri' => $uri));
  if (count($files)) {
    foreach ($files as $item) {
      // Since some database servers sometimes use a case-insensitive comparison
      // by default, double check that the filename is an exact match.
      if ($item->uri === $uri) {
        $file = $item;
        break;
      }
    }
  }

  if (!isset($file)) {
    return;
  }

  // Put your permission check code here.

  // When done perform the following code    
  if ($can_access_file) {
    $headers = file_get_content_headers($file);
    return $headers;
  }  
  return -1; 
}

my problem: once my user clicks on the link of the file google chrome starts showing the file! although they can right click on the link of the file and download that, but i want the download window appear from beginning! maybe one solution is to zip the file but what about mp4 without zipping?

0

You can do this using htaccess, Open your .htaccess file and add the following...

<FilesMatch "\.(mp4)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

This should force the browser to do a left click download, rather than play the content. You can do it for multiple files types as well. After the mp4 just add |mp3, so it would look like <FilesMatch “.(?i:(mp4|mp3|pdf))$”>

  • i added that code to the htaccess file of my localhost but did not work! – PersianMan Mar 31 '14 at 18:59
  • Did you flush your cache? – Collins Mar 31 '14 at 18:59
  • yeah but fruitless! Is there any difference between server and localhost? do i have to add the code at any spacial part of the file? – PersianMan Mar 31 '14 at 19:04
  • Try adding it to the head of the document. Or try adding just this line "AddType application/octet-stream .mp4" without the quotation marks to the head without the rest. – Collins Mar 31 '14 at 19:10
  • Please see edited answer and try that instead, it just worked for me. – Collins Mar 31 '14 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.