溫馨提示×

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

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

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

發(fā)布時(shí)間:2020-11-04 14:08:51 來(lái)源:億速云 閱讀:133 作者:小新 欄目:編程語(yǔ)言

PHP下載遠(yuǎn)程文件到指定目錄的案例?這個(gè)問(wèn)題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見(jiàn)到的。希望通過(guò)這個(gè)問(wèn)題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!

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;
   }
}

感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)PHP下載遠(yuǎn)程文件到指定目錄的案例大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道。

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

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

php
AI