一文帶你掌握PHP中常見的文件操作
一、文件讀取的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é)后停止(看先碰到那一種情況)。如果沒(méi)有指定 length,則默認(rèn)為 1K,或者說(shuō) 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)文章
php之static靜態(tài)屬性與靜態(tài)方法實(shí)例分析
這篇文章主要介紹了php之static靜態(tài)屬性與靜態(tài)方法,以實(shí)例形式較為詳細(xì)的分析了php靜態(tài)屬性與靜態(tài)方法的概念與相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07部署PHP時(shí)的4個(gè)配置修改說(shuō)明
這篇文章主要介紹了部署PHP時(shí)的4個(gè)配置修改說(shuō)明,非常重要的四點(diǎn)配置修改,希望大家認(rèn)真閱讀本文。2015-10-10thinkphp中U方法按路由規(guī)則生成url的方法
下面小編就為大家分享一篇thinkphp中U方法按路由規(guī)則生成url的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03php+ajax實(shí)現(xiàn)無(wú)刷新分頁(yè)
這篇文章主要介紹了php+ajax實(shí)現(xiàn)無(wú)刷新分頁(yè)的方法,詳細(xì)講述了Ajax文件的實(shí)現(xiàn)及PHP調(diào)用方法,需要的朋友可以參考下2015-11-11