檔案函式庫
檔案、目錄函式庫為PHP核心函式庫,可以通過其提供的API完成對于檔案及目錄的常用操作,
檔案資訊相關的API
/*
* 檔案資訊相關API
* filetype(), filesize(), filectime(),filemtime(), fileatime()
*/
$dirname = "./";
$filename = "./11.txt";
// string filetype(string filename):回傳檔案的型別
echo '檔案型別為:', filetype($dirname), "\n"; //dir
echo '檔案型別為:', filetype($filename), "\n"; //file
//int filesize(string filename):回傳檔案大小的位元組數
echo '檔案大小:', filesize($filename), "\n";
//int filectime(string filename):回傳檔案的創建時間的時間戳
echo '檔案的創建時間:', date('Y-m-d H:i:s', filectime($filename)), "\n";
//int filemtime(string filename):回傳檔案的最后修改時間的時間戳
echo '檔案的修改時間:', date('Y-m-d H:i:s', filemtime($filename)), "\n";
//int fileatime(string filename):回傳檔案的最后訪問時間的時間戳
echo '檔案的最后訪問時間:', date('Y-m-d H:i:s', fileatime($filename)), "\n";
//檢測檔案是否可讀、可寫、可執行:is_readable(), is_writeable(), is_executabel()
//var_dump(is_readable($filename)); //bool(true)
//var_dump(is_writable($filename)); //bool(true)
//var_dump(is_executable($filename)); //bool(false)
//var_dump(is_file($filename)); //bool(true)
var_dump(
is_readable($filename),
is_writable($filename),
is_executable($filename),
is_file($filename)
); //功能同上四句
檔案路徑相關API
/*
* mixed pathinfo(string $path, [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ])
* 描述:回傳檔案路徑的資訊,后面接常量表示具體的值
*
* PATHINFO_DIRNAME:檔案夾名
* PATHINFO_BASENAME:檔案全稱
* PATHINFO_EXTENSION:檔案擴展名
* PATHINFO_FILENAME:檔案名稱
*/
print_r(pathinfo($filename)); //Array([dirname] => . [basename] => 11.txt [extension] => txt [filename] => 11)
echo pathinfo($filename, PATHINFO_EXTENSION), "\n"; //取出擴展名
$filename = __FILE__;
echo pathinfo($filename, PATHINFO_DIRNAME), "\n"; //路徑部分
echo pathinfo($filename, PATHINFO_EXTENSION), "\n"; //檔案擴展名部分
//string basename(string $path[, string $suffix])
//描述:給出一個包含有指向一個檔案的全路徑的字串,回傳基本的檔案名,如果檔案名是以suffix??????結束的,那這一部分也會被去掉
echo basename($filename), "\n"; //檔案路徑下的檔案全程
//string dirname(string $path):給出一個包含有指向檔案的全路徑的字串,回傳去掉檔案名后的目錄名
echo dirname($filename), "\n"; //檔案路徑
//bool file_exists(string $filename):檢查檔案或目錄是否存在
var_dump(file_exists($filename));
檔案相關的API
//檔案創建、洗掉、剪切、重命名、拷貝
/*
* bool touch(string $filename[, int $time=time()[, int $atime]])
* 描述:如果檔案存在,則嘗試將由filename給出的檔案的訪問和修改時間設定為給出的time,
* 如果檔案不存在,則會被創建
* 引數:
* filename:要設定的檔案名
* time:要設定的時間,沒有提供則會使用當前系統的時間
* atime:如果給出了這個引數,則給定檔案的訪問時間會被設定為atime,否則設定為time
*
*/
$filename = './22.txt';
var_dump(touch($filename));
/*
* bool unlink(string $filename[, resource $context])
* 描述:洗掉指定路徑下的檔案
*
*/
if (file_exists($filename)){ //如果檔案存在則洗掉
// var_dump(unlink($filename));
}
/*
* bool rename(string $oldname, string $newname[, resource $context])
* 描述:重命名一個檔案或者目錄
*
*/
if (file_exists($filename)){ //如果檔案存在則重命名
$newName = './44.txt';
var_dump(rename($filename, $newName));
}
$oldname = '../practice';
if (file_exists($oldname)){ //如果目錄存在則重命名
$newname = '../practices';
var_dump(rename($oldname, $newname));
}
//將11.txt剪切到aaa目錄下
$filename = './11.txt';
$newname = './aaa/11.txt';
if (file_exists($newname)){
//只需要將檔案重命名就能實作其路徑的切換
var_dump(rename($newname, $filename));
}
/*
* bool copy(string $source, string $dest)
* 描述:將檔案重source拷貝到dest,如果用移動,請用rename
*
* 注意:只能移動具體檔案,不能移動目錄
*/
$filename = './11.txt';
$desname = './aaa/1.txt';
if (file_exists($filename)){
var_dump(copy($filename, $desname));
}
$imageName = 'http://pic4.nipic.com/20091217/3885730_124701000519_2.jpg';
$newName = './aaa/image1.jpg';
//拷貝遠程檔案需要開啟php.ini檔案中的allow_url_fopen=On選項
var_dump(copy($imageName, $newName)); //將http網路上的資源拷貝過來
//var_dump(rename($imageName, $newName)); //http wrapper does not support renaming 不能移動http網路上的資源
檔案內容相關API
//內容相關操作
/*
* 打開檔案
* resource fopen(string $filename, string $mode)
* 描述:打開檔案或者url
* $filename:指定的檔案名
* $mode:指定了所要求到該流的訪問型別:
* 'r':只讀方式打開,將檔案指標指向檔案頭
* 'r+':讀寫方式打開,將檔案指標指向檔案頭
* 'w':寫入方式打開,將檔案指標指向檔案頭并將檔案大小截為零
* 'w+':讀取方式打開,將檔案指標指向檔案頭并將檔案大小截為零
* 'a':寫入方式打開,將檔案指標指向檔案末尾
* 'a+':讀寫方式打開,將檔案指標指向檔案末尾
* 'x':創建并以寫入方式打開
* 'x+':創建并以讀寫方式打開
*
*
* 讀取、寫入檔案
* string fread(resource $handle, int $length)
* 描述:讀取檔案,回傳一個字串,fread()從檔案指標handle讀取最多length個位元組,
* 該函式在遇到以下幾種情況停止讀取檔案:
* 讀取了length個位元組;
* 到達了檔案末尾EOF;
*
* int ftell(resource $handle)
* 描述:回傳檔案指標讀/寫的位置
*
* int fseek(resource $handle, int $offset)
* 描述:在檔案指標中定位
*
* bool rewind(resource $handle)
* 描述:倒回檔案指標的位置,將handle的檔案位置指標設為檔案流的開頭
*
* bool ftruncate(resource $handle, int $size)
* 描述:將檔案截斷到給定的長度
*
*
* int fwrite(resource $handle, string $string[, int $length]) 注:fputs()是fwrite的別稱
* 描述:寫入檔案,把string寫入檔案指標handle處
* 如果指定了length,當寫入了length個位元組或者寫完了string以后,寫入就會停止,
* 注意:fwrite向檔案寫入內容,如果之前有內容,會產生覆寫
*
*
* 關閉檔案
* bool fclose(resource $handle)
* 描述:關閉一個已經打開的檔案指標
*
*/
$filename = '../aaa/1.txt';
//操作$handle物件的時候,要時刻注意檔案指標的位置
//將檔案內容設定為'this is a test'
$handle = fopen($filename, 'w');
fwrite($handle, 'this is a test');
fclose($handle);
/*
* 'r':只讀方式打開,將檔案指標指向檔案頭
* 'r+':讀寫方式打開,將檔案指標指向檔案頭
*/
$handle1 = fopen($filename, 'r');
echo fread($handle1, filesize($filename)), "\n";
fclose($handle1);
//注:fwrite()向檔案寫入內容,如果之前的位置有內容,會產生覆寫
$handle2 = fopen($filename, 'r+');
if (fwrite($handle2, 'aaa')){
// fseek($handle2, 0); //將檔案指標指向第一個位置
rewind($handle2); //功能同上,將檔案指標指向第一個位置
echo fread($handle2, filesize($filename)), "\n";
}
fclose($handle2);
/*
* 'w':寫入方式打開,將檔案指標指向檔案頭并將檔案大小截為零,如果檔案不存在則嘗試創建之
* 'w+':讀寫方式打開,將檔案指標指向檔案頭并將檔案大小截為零,如果檔案不存在則嘗試創建之
*/
$handle3 = fopen($filename, 'w');
fwrite($handle3, 'aaa123');
fclose($handle3);
$handle4 = fopen($filename, 'w+');
if (fwrite($handle4, 'abc')){
fseek($handle4, 0);
echo fread($handle4, filesize($filename)), "\n";
}
fclose($handle4);
/*
* 'a':寫入方式打開,將檔案指標指向檔案末尾,如果檔案存在則嘗試創建之
* 'a+':讀寫方式打開,將檔案指標指向檔案末尾,如果檔案存在則嘗試創建之
*/
//PHP_EOL:相當于'\n'換行符號
$handle5 = fopen($filename, 'a');
fwrite($handle5, 'handle5');
fclose($handle5);
$handle6 = fopen($filename, 'a+');
if (fwrite($handle6, 'handle6')){
fseek($handle6, 0);
echo fread($handle6, filesize($filename)), "\n"; //abchandleahand 疑問:為什么少了'le6'
ftruncate($handle6, 5); //將檔案截取到給定的長度
rewind($handle6);
echo fread($handle6, filesize($filename)), "\n";
}
fclose($handle6);
/*
* string fgetc(resource $handle)
* 描述:從檔案句柄中獲取一個字符,如果碰到EOF則回傳false
*
* string fgets(resource $handle[, int $length])
* 描述:從檔案指標中讀取一行
* length:限制取回該長度的資料
*
* string fgetss(resource $handle[, int $length[, string $allow_tags]])
* 描述:從檔案指標中讀取一行并過濾掉html標記,和fgets()相同,只除了fgetss()嘗試從讀取的文本中去掉任何html和php標記
* length:限制取回該長度的資料
*
* bool feof(resource $handle):測驗檔案指標是否到了檔案結束的位置
*
* string strip_tags(string $str[, string $allow_tags])
* 描述:從字串中取出html和php標記
*
* 注意:
* fgetcsv()
* fputcsv()
*
*/
$filename = '../aaa/1.txt';
//寫入一段檔案
$handle1 = fopen($filename, 'w');
$txt = <<<EOF
<h1>一級標題</h1>
<h2>二級標題</h2>
<h3>三級標題</h3>
EOF;
fwrite($handle1, $txt);
fclose($handle1);
$handle2 = fopen($filename,'r+');
echo fread($handle2, filesize($filename)), "\n";
rewind($handle2);
echo '第一個字符是:', fgetc($handle2), "\n"; //第一個字符是:<
rewind($handle2);
echo '第一行字串是:', fgets($handle2), "\n"; //第一行字串是:<h1>一級標題</h1>
rewind($handle2);
while (!feof($handle2)){ //利用feof()函式判斷是否為檔案結尾,遍歷輸出每一行
echo fgets($handle2);
}
echo "\n";
rewind($handle2);
echo fgetss($handle2), "\n"; //一級標題
rewind($handle2);
echo strip_tags(fgets($handle2)), "\n"; //功能同上
fclose($handle2);
簡化的讀取、寫入檔案
/*
* 簡化的讀取、寫入檔案
*
* string file_get_contents(string $filename[, bool $use_include_path=false[, ...]])
* 描述:將整個檔案讀入一個字串
*
* int file_put_contents(string $filename, mixed $data[, int....])
* 描述:將一個字串寫入檔案,會將之前的內容清空
* data:要寫入的資料,型別可以是string, array或者是stream資源,data可以是陣列,但不能是多緯陣列
* 一般只是寫入string型別
* 注意:如果$filename下的檔案不存在,會自動創建檔案
*
*
* 序列化和反序列化:
*
* string serialize(mixed $value)
* 描述:產生一個可存盤的值的表示,稱為序列化
*
* mixed unserialize(string $str)
* 描述:從已存盤的表示中創建php的值,稱為反序列化
*
*
* php變數和json的相互轉化:
*
* string json_encode(mixed $value...)
* 描述:對變數進行json編碼,回傳json字串
*
* mixed json_decode(string $json...)
* 描述:對json格式的字串進行解碼,轉換成php變數
*
*
*/
echo '簡化函式:', "\n";
$filename = '../aaa/1.txt';
echo file_get_contents($filename), "\n";
$string = '你好嗎,我是昭哥';
file_put_contents($filename, $string);
echo file_get_contents($filename), "\n";
$arr = [
'id' => 1001,
'name' => '王昭',
'sex' => '男',
'phone' => '1829210000',
];
$string = serialize($arr);
echo $string, "\n"; //a:5:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;i:4;i:4;i:5;} ->將$arr序列化為字串
print_r(unserialize($string)); //Array([0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5) ->將序列化后的字串反序列化為陣列
$json = json_encode($arr);
echo $json, "\n";
print_r(json_decode($json));
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/69426.html
標籤:PHP
上一篇:php陣列函式
下一篇:python對檔案的操作
