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

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

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

一、文件讀取的5種方法

1、file_get_contents: 將整個(gè)文件讀入一個(gè)字符串

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

可以讀取本地的文件

也可以用來(lái)打開一個(gè)網(wǎng)絡(luò)地址實(shí)現(xiàn)簡(jiǎn)單的網(wǎng)頁(yè)抓取

可以模擬post請(qǐng)求(stream_context_create)

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

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

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

數(shù)組的每個(gè)元素對(duì)應(yīng)于文件中的一行

可以讀取本地的文件

也可以用來(lái)讀取一個(gè)網(wǎng)絡(luò)文件

$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

從文件中讀取一行并返回長(zhǎng)度最多為 length - 1 字節(jié)的字符串。碰到換行符(包括在返回值中)、EOF 或者已經(jīng)讀取了 length - 1 字節(jié)后停止(看先碰到那一種情況)。如果沒有指定 length,則默認(rèn)為 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 — 從文件指針中讀取固定長(zhǎng)度(可安全用于二進(jìn)制文件)

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命令

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

$file = 'access.log';
$file = escapeshellarg($file); // 對(duì)命令行參數(shù)進(jìn)行安全轉(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: 寫入文件(可安全用于二進(jìn)制文件)

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 類

三、文件復(fù)制、刪除、重命名

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

可以在不同目錄間移動(dòng)

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

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

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

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

相關(guān)文章

最新評(píng)論

临江市| 社会| 邢台县| 双城市| 扶余县| 丽水市| 岚皋县| 河北区| 临海市| 文昌市| 吉隆县| 梧州市| 绥江县| 保山市| 柞水县| 德江县| 黄梅县| 泰顺县| 凤凰县| 宁海县| 博罗县| 宜昌市| 蛟河市| 河曲县| 当雄县| 海宁市| 吉木萨尔县| 安溪县| 怀安县| 象州县| 灵宝市| 广昌县| 盘山县| 长白| 墨江| 澄迈县| 昌平区| 尼木县| 额尔古纳市| 类乌齐县| 沙坪坝区|