最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文帶你掌握PHP中常見的文件操作

 更新時間:2024年03月12日 11:21:23   作者:零下兩度  
這篇文章主要為大家詳細介紹了PHP中常見的文件操作的相關(guān)知識,文字的示例代碼講解詳細,具有一定的借鑒價值,感興趣的小伙伴可以跟隨小編一起學習一下

一、文件讀取的5種方法

1、file_get_contents: 將整個文件讀入一個字符串

file_get_contents(
string $filename,
bool $use_include_path = false,
?resource $context = null,
int $offset = 0,
?int $length = null
): string|false

可以讀取本地的文件

也可以用來打開一個網(wǎng)絡地址實現(xiàn)簡單的網(wǎng)頁抓取

可以模擬post請求(stream_context_create)

$fileName = 'test.txt';
if (file_exists($fileName)) {
    $str = file_get_contents($fileName);
    echo $str;
} else {
    print_r("文件不存在");
}

2、file: 把整個文件讀入一個數(shù)組中

file(string $filename, int $flags = 0, ?resource $context = null): array|false

數(shù)組的每個元素對應于文件中的一行

可以讀取本地的文件

也可以用來讀取一個網(wǎng)絡文件

$lines = file('test.txt'); 
foreach ($lines as $line_num => $line) {
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}

3、file_open、file_gets、file_read、fclose: 從文件指針資源讀取

3.1 fgets — 從文件指針中讀取一行

fgets(resource $stream, ?int $length = null): string|false

從文件中讀取一行并返回長度最多為 length - 1 字節(jié)的字符串。碰到換行符(包括在返回值中)、EOF 或者已經(jīng)讀取了 length - 1 字節(jié)后停止(看先碰到那一種情況)。如果沒有指定 length,則默認為 1K,或者說 1024 字節(jié)。

$fp = fopen("test.txt", "r");
if ($fp) {
    while (!feof($fp)) {
        $line = fgets($fp);
        echo $line . "<br />\n";
    }
    fclose($fp);
}

3.2 fread — 從文件指針中讀取固定長度(可安全用于二進制文件)

fread(resource $stream, int $length): string|false

$fp = fopen("test.txt", "r");
if ($fp) {
    $content = "";
    while (!feof($fp)) {
        $content .= fread($fp, 1024);
    }
    #$contents = fread($handle, filesize($filename));
    echo $content;
    fclose($fp);
}

4、SplFileObject 類

https://www.php.net/manual/zh/class.splfileobject.php

5、調(diào)用linux命令

處理超大文件,比如日志文件時,可以用fseek函數(shù)定位,也可以調(diào)用linux命令處理

$file = 'access.log';
$file = escapeshellarg($file); // 對命令行參數(shù)進行安全轉(zhuǎn)義
$line = `tail -n 1 $file`;
echo $line;

二、文件寫入

1、file_put_contents: 將數(shù)據(jù)寫入文件

file_put_contents(
string $filename,
mixed $data,
int $flags = 0,
?resource $context = null
): int|false

$content = "Hello, world!"; // 要寫入文件的內(nèi)容
$file = "test.txt"; // 文件路徑

file_put_contents($file, $content);

2、fwrite: 寫入文件(可安全用于二進制文件)

fwrite(resource $stream, string $data, ?int $length = null): int|false

$content = "這是要寫入文件的內(nèi)容";
$file = fopen("test.txt", "w"); // 打開文件寫入模式
if ($file) {
    fwrite($file, $content); // 將內(nèi)容寫入文件
    fclose($file); // 關(guān)閉文件
}

3、SplFileObject 類

三、文件復制、刪除、重命名

1、copy: 拷貝文件

copy(string $from, string $to, ?resource $context = null): bool

$file = 'test.txt';
$newFile = 'test2.txt';

if (!copy($file, $newFile)) {
    echo "failed to copy $file...\n";
}

2、unlink: 刪除文件

unlink(string $filename, ?resource $context = null): bool

$fileName = 'test2.txt';
if (file_exists($fileName)) {
   if (unlink($fileName)) {
       echo '刪除成功';
   }
}

3、rename: 重命名文件

rename(string $from, string $to, ?resource $context = null): bool

可以在不同目錄間移動

如果重命名文件時 to 已經(jīng)存在,將會覆蓋掉它

如果重命名文件夾時 to 已經(jīng)存在,本函數(shù)將導致一個警告

$fileName = 'test.txt';
$rename = 'test_new.txt';
if (file_exists($fileName)) {
   if (rename($fileName, $rename )) {
       echo '重命名成功';
   }
}

到此這篇關(guān)于一文帶你掌握PHP中常見的文件操作的文章就介紹到這了,更多相關(guān)PHP文件操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

潞西市| 乐都县| 资中县| 法库县| 清原| 揭东县| 神农架林区| 和田市| 察雅县| 贡山| 临武县| 体育| 锡林浩特市| 都兰县| 运城市| 七台河市| 白玉县| 剑川县| 莱西市| 高要市| 肥西县| 武强县| 上高县| 莒南县| 商南县| 常熟市| 荆州市| 错那县| 汾阳市| 石首市| 凌海市| 淄博市| 始兴县| 金川县| 织金县| 靖州| 桃园市| 烟台市| 淮南市| 镇安县| 昭觉县|