PHP 檔案處理

讀檔:

1.fopen

範例:

<?php
$handle = fopen("open_file.txt", "r");
if( $handle === FALSE)
{    
    echo "Open File Fail" . "<br>";
}
else
{    
    echo "Open File Success" . "<br>";
    while (!feof($handle))
    {
        $current_line = fgets($handle);
        echo $current_line . "<br>";
    }
}
    
?>

2.file_get_contents

範例:

<?php

$handle = file_get_contents('open_file.txt');
echo $handle;

?>

=========================================================

寫檔:

1.fwrite

<?php
$handle = fopen('open_file.txt', 'w');
fwrite($handle, "test1");
fclose($fp);
?>

ps:fwrite 是沒有加換行字元,如果要換行必須加上 \n 處理

 

2.file_put_contents

<?php
    $handle = 'open_file.txt';
    $current_line = "line1\n";
    file_put_contents($handle, $current_line);
?>

 

 

 

 

arrow
arrow
    全站熱搜

    yuanann 發表在 痞客邦 留言(0) 人氣()