PHP讀取遠端檔案的4種方法

2022-04-29 02:33:05 字數 1648 閱讀 5897

1. fopen, fread

1 if($file = fopen("", "r"))

5 fclose($file);

2. file_get_contents

很簡單的一句話:

$data = file_get_contents("");

如果要限制超時時間,需要使用到它的$context引數

其中,第二個引數$use_include_path表示在php.ini設定的include_path中查詢檔案,使用false即可。

此外,本函式也可以傳送post資料:

相對來說第二種方法比較快捷。以上兩種方法需要php.ini設定allow_url_fopen=on。

3. fsockopen, fwrite, fread

01 if($fp = fsockopen('www.example.com', 80, $errno, $errstr, 30))

本方法需要開啟php_sockets擴充套件

4. curl

1 $curl = curl_init();

2 curl_setopt($curl, curlopt_url, "");

3 curl_setopt($curl, curlopt_returntransfer, 1);

4 curl_setopt($curl, curlopt_timeout, 30);

5 $data = curl_exec($curl);

6 curl_close($curl);

curl也可用來傳送post資料及傳送http請求頭資訊,以下是另一用例:

本方法需要開啟php_curl擴充套件

PHP之讀取遠端檔案的三種方法

1.curl init exec url ch curl init timeout 5 curl setopt ch,curlopt url,url curl setopt ch,curlopt returntransfer,1 curl setopt ch,curlopt connecttimeo...

Shell逐行讀取檔案的4種方法

這篇文章主要介紹了shell逐行讀取檔案的4種方法,本文介紹了while迴圈法 重定向法 管道法 檔案描述符法等一些方法,需要的朋友可以參考下 在linux中有很多方法逐行讀取乙個檔案的方法,其中最常用的就是下面的指令碼裡的方法,而且是效率最高,使用最多的方法。為了給大家乙個直觀的感受,我們將通過生...

php讀取檔案內容的三種方法

第一種讀取方式 如下 header content type text html charset utf 8 檔案路徑 file path text.txt 判斷是否有這個檔案 if file exists file path else else fclose fp 第二種讀取方式 如下 heade...