一文帶你掌握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é)后停止(看先碰到那一種情況)。如果沒有指定 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)文章
PHP5.6.8連接SQL Server 2008 R2數(shù)據(jù)庫(kù)常用技巧分析總結(jié)
這篇文章主要介紹了PHP5.6.8連接SQL Server 2008 R2數(shù)據(jù)庫(kù)常用技巧,結(jié)合實(shí)例形式分析總結(jié)了PHP5.6.8連接SQL Server 2008 R2數(shù)據(jù)庫(kù)操作步驟、遇到的問題及相應(yīng)的解決方法,需要的朋友可以參考下2019-05-05
實(shí)例講解通過PHP創(chuàng)建數(shù)據(jù)庫(kù)
在本篇文章里小編給大家分享了關(guān)于如何通過​PHP創(chuàng)建數(shù)據(jù)庫(kù)的知識(shí)點(diǎn)內(nèi)容,有需要的朋友們學(xué)習(xí)下。2019-01-01
PHP 登錄完成后如何跳轉(zhuǎn)上一訪問頁(yè)面
訪問網(wǎng)站頁(yè)面時(shí),有的頁(yè)面需要授權(quán)才能訪問,這時(shí)候就會(huì)要求用戶登錄,跳轉(zhuǎn)到登錄頁(yè)面login.php,怎么實(shí)現(xiàn)登錄后返回到剛才訪問的頁(yè)面2014-01-01
解析關(guān)于java,php以及html的所有文件編碼與亂碼的處理方法匯總
本篇文章是對(duì)關(guān)于java,php以及html的所有文件編碼與亂碼的處理方法進(jìn)行了詳細(xì)的總結(jié)與介紹,需要的朋友參考下2013-06-06
無(wú)刷新動(dòng)態(tài)加載數(shù)據(jù) 滾動(dòng)條加載適合評(píng)論等頁(yè)面
無(wú)刷新動(dòng)態(tài)加載數(shù)據(jù),滾屏加載更多數(shù)據(jù),適合評(píng)論等頁(yè)面,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下2013-10-10
php實(shí)現(xiàn)根據(jù)字符串生成對(duì)應(yīng)數(shù)組的方法
這篇文章主要介紹了php實(shí)現(xiàn)根據(jù)字符串生成對(duì)應(yīng)數(shù)組的方法,包含了數(shù)組操作的技巧及eval函數(shù)的用法,需要的朋友可以參考下2014-09-09

