淺談php fopen下載遠程檔案的函數

來源:互聯網
上載者:User
如下所示:

//下載附件function get_file($url, $folder = "./") {    set_time_limit (24 * 60 * 60); // 設定逾時時間    $destination_folder = $folder . '/'; // 檔案下載儲存目錄,預設為當前檔案目錄    if (!is_dir($destination_folder)) { // 判斷目錄是否存在        mkdirs($destination_folder); // 如果沒有就建立目錄    }    $newfname = $destination_folder . basename($url); // 取得檔案的名稱    $file = fopen ($url, "rb"); // 遠程下載檔案,二進位模式    if ($file) { // 如果下載成功        $newf = fopen ($newfname, "wb"); // 遠在檔案檔案      if ($newf) // 如果檔案儲存成功        while (!feof($file)) { // 判斷附件寫入是否完整          fwrite($newf, fread($file, 1024 * 8), 1024 * 8); // 沒有寫完就繼續      }    }    if ($file) {      fclose($file); // 關閉遠程檔案    }    if ($newf) {      fclose($newf); // 關閉本地檔案    }    return true;  }  function mkdirs($path , $mode = "0755") {    if (!is_dir($path)) { // 判斷目錄是否存在        mkdirs(dirname($path), $mode); // 迴圈建立目錄       mkdir($path, $mode); // 建立目錄    }    return true;   }    // 使用樣本  echo get_file('xxxxx.pdf');

以上這篇淺談php fopen下載遠程檔案的函數就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援topic.alibabacloud.com。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.