{"id":1657,"date":"2015-09-28T16:19:32","date_gmt":"2015-09-28T08:19:32","guid":{"rendered":"http:\/\/clear.vimge.com\/?p=1657"},"modified":"2015-09-28T16:23:51","modified_gmt":"2015-09-28T08:23:51","slug":"php-download-file","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/php\/php-download-file.html","title":{"rendered":"\u5173\u4e8ePHP\u505a\u6587\u4ef6\u4e0b\u8f7d\u6548\u7387\u4f18\u5316\u548c\u4f53\u9a8c\u63d0\u5347"},"content":{"rendered":"

Web\u5f00\u53d1\u4e2d\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981PHP\u5904\u7406\u8981\u4e0b\u8f7d\u7684\u6587\u4ef6\uff0c\u6bd4\u5982\u9700\u8981\u7528\u6237\u767b\u9646\u624d\u80fd\u4e0b\u8f7d\u6587\u4ef6\uff0c\u88ab\u4e0b\u8f7d\u7684\u6587\u4ef6\u4e0d\u5141\u8bb8\u76f4\u63a5\u8bbf\u95ee\uff0c\u4e0b\u8f7d\u540e\u7684\u6587\u4ef6\u9700\u8981\u91cd\u547d\u540d\u7b49\uff0c\u901a\u5e38\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u5b9e\u73b0\uff1a<\/p>\n

<?php\r\n  authenticate(); \/\/\u6743\u9650\u5224\u65ad\r\n  \/\/ \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\r\n  $content=file_get_contents($file);\r\n  \/\/ \u53d1\u9001\u5408\u9002\u7684 HTTP \u5934\r\n  header(\"Content-type: application\/octet-stream\");\r\n  header('Content-Disposition: attachment; filename=\"' . basename($file) . '\"');\r\n  header(\"Content-Length: \". filesize($file));\r\n  echo $content; \/\/ \u6216\u8005 readfile($file);\r\n?><\/pre>\n

\u4f46\u662f\u8fd9\u6837\u505a\u6027\u80fd\u6781\u4f4e\uff0c\u4f60\u53ef\u4ee5\u60f3\u50cf\u7528\u6237\u6bcf\u6b21\u4e0b\u8f7d\u7684\u65f6\u5019\u7a0b\u5e8f\u90fd\u4f1a\u4e00\u6b21\u4e00\u6b21\u5730\u7ecf\u8fc7\u4e00\u4e2a\u56fa\u5b9a\u7684buffer\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5230\u5185\u5b58\uff0c\u518d\u53d1\u9001\u5230\u524d\u7aef\u670d\u52a1\u5668\uff0c\u6700\u540e\u624d\u80fd\u53d1\u9001\u5230\u5ba2\u6237\u7aef\uff0c\u8fd8\u6709\u5404\u79cd\u7f51\u7edc\u8d85\u65f6\u548c\u5927\u91cf\u5185\u5b58\u6d88\u8017\uff0c\u8fd9\u5c06\u4f1a\u662f\u4e00\u4e2a\u76f8\u5f53\u7cdf\u7cd5\u7684\u8bbe\u8ba1\u3002\u4e0b\u9762\u5c06\u4ecb\u7ecd\u4e00\u4e2a\u66f4\u76f4\u63a5\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n

X-Sendfile \u662f\u4e00\u79cd\u5c06\u6587\u4ef6\u4e0b\u8f7d\u8bf7\u6c42\u7531\u540e\u7aef\u8f6c\u4ea4\u7ed9\u524d\u7aef\u670d\u52a1\u5668\uff0c\u7531\u524d\u7aef\u670d\u52a1\u5668\u76f4\u63a5\u5904\u7406\u6587\u4ef6\u4e0b\u8f7d\u8bf7\u6c42\u7684\u4e00\u79cd\u673a\u5236\u3002\u5b83\u53ef\u4ee5\u6709\u6548\u7f13\u89e3\u540e\u7aef\u670d\u52a1\u5668\u538b\u529b\uff0c\u5c06\u6587\u4ef6\u4e0b\u8f7d\u8bf7\u6c42\u76f4\u63a5\u5c06\u7531\u64c5\u957f\u5904\u7406\u9759\u6001\u6587\u4ef6\u8bf7\u6c42\u7684\u524d\u7aef\u670d\u52a1\u5668\u5982\uff1aNginx\u3001Lighthttpd\u7b49\uff0c\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u6587\u4ef6\u4e0b\u8f7d\u8bf7\u6c42\u6548\u7387\u3002<\/p>\n

X-Sendfile\u662f\u901a\u8fc7\u4e00\u4e2a\u975e\u6807\u51c6\u5316\u7684\u7279\u5b9aHTTP header\u5b9e\u73b0\u7684\uff0c\u540e\u7aef\u670d\u52a1\u5668\u53d1\u9001\u4e00\u4e2a\u7279\u5b9a\u7684HTTP header\u5230\u524d\u7aef\u670d\u52a1\u5668\u7684\u65f6\u5019\uff0c\u524d\u7aef\u670d\u52a1\u5668\u4eceX-Sendfile\u5f97\u5230\u8bf7\u6c42\u7684\u6587\u4ef6\u5730\u5740\u540e\uff0c\u524d\u7aef\u670d\u52a1\u5668\u5c06\u5229\u7528\u81ea\u8eab\u7684\u529f\u80fd\u673a\u5236\u5c06\u6587\u4ef6\u76f4\u63a5\u53d1\u9001\u7ed9\u7528\u6237\u3002<\/p>\n

X-Sendfile\u5b9e\u9645\u4e0a\u662f\u4e00\u79cd\u670d\u52a1\u5668\u5185\u90e8\u7684\u8df3\u8f6c\u673a\u5236\uff0c\u8fd9\u79cd\u5904\u7406\u8bf7\u6c42\u5e76\u4e0d\u4f1a\u5c06\u5b9e\u9645\u9700\u8981\u8bf7\u6c42\u7684\u6587\u4ef6\u5730\u5740\u66b4\u9732\u7ed9\u7528\u6237\uff0cX-Sendfile\u505a\u4e3a\u4e00\u79cd\u975e\u6807\u51c6\u5316\u7684\u673a\u5236\uff0c\u4e0d\u540c\u7684\u670d\u52a1\u5668\u7684\u5b9e\u73b0\u6709\u6240\u533a\u522b<\/p>\n\n\n\n\n\n\n
SENDFILE \u5934<\/th>\n\u4f7f\u7528\u7684 WEB \u5668<\/th>\n<\/tr>\n
X-Sendfile<\/td>\nApache, Lighttpd v1.5, Cherokee<\/td>\n<\/tr>\n
X-LIGHTTPD-send-file<\/td>\nLighttpd v1.4<\/td>\n<\/tr>\n
X-Accel-Redirect<\/td>\nNginx, Cherokee<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u4e0b\u9762\u4ec5\u4ee5Nginx\u4e3a\u4f8b\u8bb2\u89e3\u5982\u4f55\u914d\u7f6e<\/p>\n

Nginx\u9ed8\u8ba4\u662f\u652f\u6301X-Sendfiler\u7279\u6027\u7684\uff0c\u4e0d\u9700\u8981\u989d\u5916\u52a0\u8f7d\u6a21\u5757\uff0c\u670d\u52a1\u5668\u7aef\u914d\u7f6e\u5982\u4e0b<\/p>\n

\n
location \/down {\r\n  internal;\r\n  root  \/home\/www;\r\n}<\/pre>\n<\/blockquote>\n

\u540e\u7aefPHP\u7a0b\u5e8f\u9700\u8981\u4f7f\u7528header\u53d1\u9001HTTP\u5934X-Accel-Redirec\uff0c\u4ee5\u4e0b\u662fdownload.php\u6587\u4ef6<\/p>\n

\n
<?php\r\n  $filePath = '\/down\/hello.pdf';\r\n  header('Content-type: application\/octet-stream');\r\n  header('Content-Disposition: attachment; filename=\"' . basename($filePath) . '\"');\/\/\u6587\u4ef6<\/span>\r\n  header('X-Accel-Redirect: '.$filePath);\r\n?><\/pre>\n<\/blockquote>\n

\u8fd9\u6837\u5f53\u8bbf\u95ee http:\/\/domain\/download.php \u7684\u65f6\u5019\u5373\u4e0b\u8f7d \/home\/www\/down<\/span>\/hello.pdf\u6587\u4ef6<\/p>\n

\u5982\u679c\u7528\u6237\u60f3\u4e0b\u8f7d \/home\/www\/hello.pdf \u6587\u4ef6\u7684\u8bdd\uff0c\u9700\u8981\u4f7f\u7528nginx\u7684alias\u5173\u952e\u5b57\uff0c\u5177\u4f53\u914d\u7f6e\u5982\u4e0b<\/p>\n

\n
location \/down {\r\n  internal;\r\n  alias  \/home\/www;\r\n}<\/pre>\n<\/blockquote>\n

\u53e6\u5916\u6709\u65f6\u5019\u5728\u90e8\u5206\u6d4f\u89c8\u5668\u4e0a\u9762\u4f1a\u51fa\u73b0\u76f4\u63a5\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00PDF\u6587\u4ef6\uff0c\u800c\u4e0d\u662f\u53e6\u5b58\u4e3a\u6587\u4ef6\u7684\u95ee\u9898\uff0c\u53ea\u9700\u8981\u8bbe\u7f6e\u4e00\u4e0b\u670d\u52a1\u5668\u54cd\u5e94\u7684MIME\uff0c\u53ef\u901a\u8fc7Nginx\u6216\u8005PHP\u5b9e\u73b0<\/p>\n

Nginx\u914d\u7f6e\u5b9e\u73b0\uff1a<\/p>\n

                \r\n  if ($request_filename ~* ^.*?\\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx)$){\r\n    add_header Content-Type 'application\/octet-stream';\r\n  }<\/pre>\n

\u5728location\u914d\u7f6e\u9879\u4e2d\u589e\u52a0\u4ee5\u4e0a\u914d\u7f6e\uff0c\u901a\u8fc7\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u5b9e\u73b0\u5bf9\u7279\u5b9a\u540e\u7f00\u7684\u6587\u4ef6\u5b9e\u73b0\u6d4f\u89c8\u5668\u6253\u5f00\u300e\u53e6\u5b58\u4e3a\u300f\u7a97\u53e3<\/p>\n

PHP\u5b9e\u73b0\uff1a<\/p>\n

header(<\/code>\"Content-type: application\/octet-stream\"<\/code>);<\/code><\/p><\/blockquote>\n

\u5728\u6587\u4ef6\u8f93\u51fa\u4e2d\u589e\u52a0\u7279\u5b9a\u7684Header\u5934\uff0c\u6ce8\u610f\u5728\u9875\u9762\u8df3\u8f6c\u4e4b\u524d\u8f93\u51fa\u4ee5\u4e0a\u5934\u65e0\u6548\uff0c\u5305\u62ec\u5728header(“Location http:\/\/domain\/hello.pdf”) \u8bed\u53e5\uff0c\u800c\u5bf9\u4e8e\u4e0a\u9762X-Accel-Redirect\u5b9e\u73b0\u7684\u5185\u90e8\u8df3\u8f6c\u4e2d\u5219\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n

\u53c2\u8003\u8d44\u6599<\/p>\n

https:\/\/www.nginx.com\/resources\/wiki\/start\/topics\/examples\/x-accel\/#x-accel-redirect
\nhttp:\/\/www.laruence.com\/2012\/05\/02\/2613.html<\/p>\n","protected":false},"excerpt":{"rendered":"

Web\u5f00\u53d1\u4e2d\u6211\u4eec\u7ecf\u5e38\u4f1a\u9047\u5230\u9700\u8981PHP\u5904\u7406\u8981\u4e0b\u8f7d\u7684\u6587\u4ef6\uff0c\u6bd4\u5982\u9700\u8981\u7528\u6237\u767b\u9646\u624d\u80fd\u4e0b\u8f7d\u6587\u4ef6\uff0c\u88ab\u4e0b\u8f7d\u7684\u6587\u4ef6\u4e0d\u5141\u8bb8\u76f4\u63a5\u8bbf\u95ee […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[33,131,130],"_links":{"self":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1657"}],"collection":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/comments?post=1657"}],"version-history":[{"count":4,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1657\/revisions"}],"predecessor-version":[{"id":1660,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1657\/revisions\/1660"}],"wp:attachment":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/media?parent=1657"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/categories?post=1657"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/tags?post=1657"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}