溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PHP下載遠(yuǎn)程文件到指定目錄的方法

發(fā)布時(shí)間:2020-05-23 14:19:07 來源:億速云 閱讀:330 作者:鴿子 欄目:編程語言

PHP用curl可以輕松實(shí)現(xiàn)下載遠(yuǎn)程文件到指定目錄:

<?php
class Download
{
   public static function get($url, $file)
   {
      return file_put_contents($file, file_get_contents($url));
   }
   
   public static function curlGet($url, $file)
   {
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_POST, 0); 
      curl_setopt($ch,CURLOPT_URL,$url); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
      $file_content = curl_exec($ch);
      curl_close($ch);
      $downloaded_file = fopen($file, 'w');
      fwrite($downloaded_file, $file_content);
      fclose($downloaded_file);
   }
   
   public static function openGet($url, $file)
   {
      $in = fopen($url, "rb");
      $out = fopen($file, "wb");
      while ($chunk = fread($in,8192))
      {
         fwrite($out, $chunk, 8192);
      }
      fclose($in);
      fclose($out);
   }
   
   /**
   *
   * 創(chuàng)建目錄,支持遞歸創(chuàng)建目錄
   * @param String $dirName 要?jiǎng)?chuàng)建的目錄
   * @param int $mode 目錄權(quán)限
   */
   public static function smkdir($dirName , $mode = 0777) {
     $dirs = explode('/' , str_replace('\\' , '/' , $dirName));
     $dir = '';
     foreach ($dirs as $part) {
        $dir.=$part . '/';
        if ( ! is_dir($dir) && strlen($dir) > 0) {
           if ( ! mkdir($dir , $mode)) {
              return false;
           }
           if ( ! chmod($dir , $mode)) {
              return false;
           }
        }
     }
     return true;
   }
}

以上就是PHP如何下載遠(yuǎn)程文件到指定目錄的詳細(xì)內(nèi)容,更多請關(guān)注億速云其它相關(guān)文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI