php讀取本地檔案操作函數_PHP教程

來源:互聯網
上載者:User
在php中讀取本地檔案我們最常用的就是fopen與fread函數配置使用即可了,還有一些其它的像php file_get_contents可以讀本地檔案也可以讀遠程檔案了,下面我們來一一介紹一下。

fopen() 函數
直接開啟檔案

代碼如下 複製代碼

$file = "111.txt";
$fp = fopen($file,"r");
if ($fp){
while(!feof($fp)){
//第二個參數為讀取的長度
$data = fread($fp, 1000);
}
fclose($fp);
}
echo $data;
?>

file_get_contents() 函數把整個檔案讀入一個字串中

例子

代碼如下 複製代碼

echo file_get_contents("test.txt");
?>

輸出:

This is a test file with test text.

php讀取本地檔案夾檔案

代碼如下 複製代碼

$dir = opendir('/movie');
while(($file = readdir($dir))!=false){
if ($file!="." && $file!="..") {
$ns = explode('.', $file);
echo $ns[0];
}
}
closedir($dir);

?>


http://www.bkjia.com/PHPjc/445626.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445626.htmlTechArticle在php中讀取本地檔案我們最常用的就是fopen與fread函數配置使用即可了,還有一些其它的像php file_get_contents可以讀本地檔案也可以讀遠程檔案...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.