欢迎各位兄弟 发布技术文章
这里的技术是共享的
function mymodule_download_file(){ } 函数里面的如果用php原生的代码的话,见 /node-admin/1564 php原生代码吧!
New quick post to share a feature.
To force to download a file, as usual with Drupal, there's a module for that. In this case there are even two: Download file and File force. But you may be reticent to install a module for such a basic feature! So here is the solution in a few lines of code:
Here is a snippet based on File API. It is more secure than the second one but it will not work on files not handled by Drupal (whatever file download method you're using) : thus I recommend it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php /** * Implementation of hook_menu() */ function mymodule_menu() { $items['download/%file'] = array( 'page callback' => 'mymodule_download_file', 'access arguments' => array('administer site configuration'), 'type' => MENU_CALLBACK, ); return $items; } /** * Page callback for forcing a file to download */ function mymodule_download_file($file) { if($file) { //drupal6的file_transfer与drupal7的稍有不同,见 /node-admin/8900 file_transfer($file->uri, array('Content-disposition' => 'attachment; filename='.$file->filename)); } else { return drupal_access_denied(); } } //下面是drupal6的代码 自己亲自做的 //下面是下载功能 肯定有用 function shipingzhongcustomtask_chakantuorfile($fid){ $file = field_file_load($fid); //session_cache_limiter("public"); // for IE 好像 file_transfer 里面已处理了 if($file) { //下面是下载功能 $arr_headers = array( 'Content-type: application/octet-stream', 'Accept-Ranges: bytes', 'Content-Length: '.filesize(file_create_path($file['filepath'])), 'Content-disposition: attachment; filename='.$file['filename'].';' ); //var_dump($_SERVER['DOCUMENT_ROOT'] . base_path() . file_create_path($file['filepath']));exit; file_transfer(file_create_path($file['filepath']), $arr_headers); } else { return drupal_access_denied(); } } //下面是drupal6的代码 自己亲自做的 //下面是在线浏览器直接打开功能 肯定有用 function shipingzhongcustomtask_chakantuorfile($fid){ $file = field_file_load($fid); //session_cache_limiter("public"); // for IE 好像 file_transfer 里面已处理了 if($file) { //下面是浏览器在线直接打开 $type = file_get_mimetype($file['filename']); $arr_headers = array( 'Pragma: public', 'Expires: 0', 'Cache-Control: must-revalidate, post-check=0, pre-check=0', 'Cache-Control: public', 'Content-Type: '.$type, 'Content-Disposition: inline; filename=' . $file['filename'] . ';', 'Content-Transfer-Encoding: binary', ); //var_dump($_SERVER['DOCUMENT_ROOT'] . base_path() . file_create_path($file['filepath']));exit; file_transfer(file_create_path($file['filepath']), $arr_headers); } else { return drupal_access_denied(); } } |
You can download the file with a link like http://mysite.com/download/123 where 123 is the file id (fid) of the file being downloaded.
This second snippet may present a security issue! It is recommended to use it as a last resort when files are not handled by Drupal. To increase security, this feature will be restricted to administrators, and filtered on file extensions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php /** * Implementation of hook_menu() */ function mymodule_menu() { $items['download'] = array( 'page callback' => 'mymodule_download_file', 'access arguments' => array('administer site configuration'), 'type' => MENU_CALLBACK, ); return $items; } /** * Page callback for forcing a file to download */ // 下面的函数好像是有点问题 如果用php原生的代码的话,见 /node-admin/1564 php原生代码吧! function mymodule_download_file() { if (!isset($_GET['file']) || !file_exists($_GET['file'])) { return drupal_not_found(); } $filepath = $_GET['file']; $realpath = realpath($path); $filename = basename($filepath); $extension = pathinfo($filepath, PATHINFO_EXTENSION); // Check extension and restrict to files in DRUPAL_ROOT if(in_array($extension, array('jpg', 'png', 'gif', 'mp4')) && substr($path, 0, strlen(DRUPAL_ROOT)) === DRUPAL_ROOT) { drupal_add_http_header('Content-disposition', 'attachment; filename=' . $filename); readfile($filepath); } else { return drupal_access_denied(); } } |
Then you can create your file link as in http://mysite.com/download?file=sites/default/files/myimage.png
It's only working for the public file system handling but it is the most famous one.
来自 http://blog.sebcorbin.fr/en/how-force-downloading-file-drupal-7