{"id":49,"date":"2021-12-31T15:49:53","date_gmt":"2021-12-31T07:49:53","guid":{"rendered":"http:\/\/pkant.com\/?p=49"},"modified":"2021-12-31T15:53:39","modified_gmt":"2021-12-31T07:53:39","slug":"php-%e4%b8%8b%e8%bd%bd%e8%bf%9c%e7%a8%8b%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/jwever.cn\/index.php\/2021\/12\/31\/php-%e4%b8%8b%e8%bd%bd%e8%bf%9c%e7%a8%8b%e6%96%87%e4%bb%b6\/","title":{"rendered":"PHP \u4e0b\u8f7d\u8fdc\u7a0b\u6587\u4ef6\u8f93\u51fa\u5230\u6d4f\u89c8\u5668"},"content":{"rendered":"<pre class=\"prettyprint linenums\" ><code class=\"language-php\">&lt;?php\n\n\/**\n * @description  \u6587\u4ef6\u4e0b\u8f7d \u652f\u6301\u672c\u5730\u548c\u8fdc\u7a0b\u6587\u4ef6(\u6d4f\u89c8\u5668\u76f4\u63a5\u4e0b\u8f7d)\n * @param        $fileUrl  \u672c\u5730\u6587\u4ef6\u7edd\u5bf9\u8def\u5f84 OR \u8fdc\u7a0b\u6587\u4ef6\u5730\u5740\n * @param string $fileName \u4e0b\u8f7d\u4fdd\u5b58\u7684\u6587\u4ef6\u540d\n *\/\nfunction download_file($fileUrl, $fileName = &#039;&#039;){\n    if(!$fileUrl){\n        return false;\n    }\n    if ($fileName == &#039;&#039;) {\n        $pathInfo = pathinfo($fileUrl);\n        $fileName = $pathInfo[&#039;basename&#039;];\n    }\n\n    if (!headers_sent()) {\n        \/\/\u76f4\u63a5\u4e0b\u8f7d\u672c\u5730\u6587\u4ef6\n        if (file_exists($fileUrl)) {\n            $fileOpen = fopen($fileUrl, &#039;rb&#039;);\n            $fileSize = filesize($fileUrl);\n            header(&quot;Cache-Control: public&quot;);\/\/\u7f13\u5b58\u63a7\u5236\n            header(&quot;Content-Type: application\/octet-stream&quot;);\n            header(&quot;Accept-Ranges: bytes&quot;);\n            header(&quot;Content-Length: &quot; . $fileSize);\n            header(&#039;Access-Control-Expose-Headers: Content-Disposition&#039;);\n            header(&quot;Content-Disposition: attachment; filename=&quot; . $fileName);\n            $chunk_size = 4096; \/\/ \u9488\u5bf9\u5927\u6587\u4ef6\uff0c\u89c4\u5b9a\u6bcf\u6b21\u8bfb\u53d6\u6587\u4ef6\u7684\u5b57\u8282\u6570\u4e3a4096\u5b57\u8282\uff0c\u76f4\u63a5\u8f93\u51fa\u6570\u636e\n            $sum_buffer = 0; \/\/\u603b\u7684\u7f13\u51b2\u7684\u5b57\u8282\u6570\n            ob_start(); \/\/ \u5f00\u542f\u7f13\u51b2\u533a\n            \/\/\u53ea\u8981\u6ca1\u5230\u6587\u4ef6\u5c3e\uff0c\u5c31\u4e00\u76f4\u8bfb\u53d6\n            while (!feof($fileOpen) &amp;&amp; $sum_buffer &lt; $fileSize) {\n                echo fread($fileOpen, $chunk_size);\n                $sum_buffer += $chunk_size;\n                ob_flush(); \/\/ \u5237\u65b0PHP\u7f13\u51b2\u533a\u5230Web\u670d\u52a1\u5668\n                flush(); \/\/ \u5237\u65b0Web\u670d\u52a1\u5668\u7f13\u51b2\u533a\u5230\u6d4f\u89c8\u5668\n            }\n            ob_end_clean(); \/\/ \u5173\u95ed\u7f13\u51b2\u533a\n            fclose($fileOpen);\n            exit;\n        }else{\n            \/\/\u76f4\u63a5\u4e0b\u8f7d\u8fdc\u7a0b\u6587\u4ef6\u5e76\u8f93\u51fa\u5230\u6d4f\u89c8\u5668\n            ob_start();\n            $ch = curl_init();\n            curl_setopt($ch, CURLOPT_URL, $fileUrl);\n            curl_setopt($ch, CURLOPT_REFERER, &quot;&quot;);\n            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n            curl_setopt($ch, CURLOPT_HEADER, 0);\n            $cexecute = curl_exec($ch);\n            curl_close($ch);\n            header(&#039;Content-Disposition: attachment; filename=&quot;&#039;.$fileName.&#039;&quot;&#039;);\n            header(&#039;Content-Length:&#039;.strlen($cexecute));\n            echo $cexecute;\n            ob_flush();\n            flush();\n            ob_end_clean();\n            exit;\n        }\n    }else{\n        exit;\n    }\n\n}\n\n\/**\n * @description  \u4e0b\u8f7d\u8fdc\u7a0b\u6587\u4ef6\u5230\u672c\u5730\n * @param        $fileUrl\n * @param string $savaDir\n * @return bool|string\n *\/\nfunction save_file($fileUrl,$savaDir=&#039;uploads\/temporary\/&#039;){\n    if(!$fileUrl){\n        return false;\n    }\n    $savaDir = $savaDir ? rtrim($savaDir,&#039;\/&#039;).&#039;\/&#039; : &#039;&#039;;\n    $localTemporaryDir = FILE_UPLOAD.$savaDir;\/\/\u6b64\u5904\u6ce8\u610f\u6539\u6210\u81ea\u5df1\u9879\u76ee\u914d\u7f6e\u7684\u6587\u4ef6\u4e0b\u8f7d\u76ee\u5f55\n    if(!is_dir($localTemporaryDir)){\n        mkdir($localTemporaryDir,0777,true);\n    }\n\n    $baseName = pathinfo($fileUrl)[&#039;basename&#039;];\n    $fName = substr($baseName,0,strpos($baseName, &#039;?&#039;));\/\/\u53bb\u9664\u6587\u4ef6\u7684\u7b7e\u540d\u76f8\u5173\u53c2\u6570\n    $localFile = $localTemporaryDir . $fName;\n\n    if(file_exists($localFile)){\n        unlink($localFile);\n    }\n    $ch = curl_init();\n    curl_setopt($ch, CURLOPT_URL, $fileUrl);\n    curl_setopt($ch, CURLOPT_HEADER, FALSE);\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\n    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 900);\n    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);\n    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);\n    curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);\n    $file = curl_exec($ch);\n    curl_close($ch);\n    if(!strpos($file, &#039;&lt;Error&gt;&#039;)){\n        file_put_contents($localFile, $file);\n    }\n    if (file_exists($localFile)) {\n        return $localFile;\n    }\n    return false;\n}\n\n?&gt;\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php \/** * @description \u6587\u4ef6\u4e0b\u8f7d \u652f\u6301\u672c\u5730\u548c\u8fdc\u7a0b\u6587\u4ef6(\u6d4f\u89c8\u5668\u76f4\u63a5\u4e0b\u8f7d) * @ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-houduan"],"_links":{"self":[{"href":"https:\/\/jwever.cn\/index.php\/wp-json\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jwever.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jwever.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jwever.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jwever.cn\/index.php\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":2,"href":"https:\/\/jwever.cn\/index.php\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/jwever.cn\/index.php\/wp-json\/wp\/v2\/posts\/49\/revisions\/51"}],"wp:attachment":[{"href":"https:\/\/jwever.cn\/index.php\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jwever.cn\/index.php\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jwever.cn\/index.php\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}