H5學習_番外篇_PHP數據庫操作
1. 文件操作
1.1 打開關閉文件
fopen()
resource fopen ( string filename, string mode [, bool use_include_path [, resource zcontext]] )
?fopen()函數將resource綁定到一個流或句柄。綁定之後。腳本就能夠通過句柄與此資源交互;
例1:以僅僅讀方式打開一個位於本地server的文本文件
$fh = fopen("test.txt", "r");
例2:以僅僅讀方式打開一個遠程文件
$fh = fopen("http://www.baidu.com", "r");
fclose()
bool fclose ( resource handle )
將 handle 指向的文件關閉 。假設成功則返回 TRUE。失敗則返回 FALSE;
文件指針必須有效,而且是通過 fopen() 或 fsockopen() 成功打開的;
盡管每一個請求最後都會自己主動關閉文件。但明白的關閉打開的全部文件是一個好的習慣;
例:
$fh = fopen("test.txt", "r"); fclose($fh);
1.2 讀取文件
php 提供了非常多從文件裏讀取數據的方法,不僅能夠一次僅僅讀取一個字符。還能夠一次讀取整個文件。
fread()
string fread ( int handle, int length )
fread()函數從handle指定的資源中讀取length個字符,當到達EOF或讀取到length個字符時讀取將停止。
假設要讀取整個文件,使用filesize()函數確定應該讀取的字符數;
例:
$file = "test.txt"; $fh = fopen( $file, "r"); $str = fread($fh, filesize($file)); echo $str; fclose($fh);
fgets()
string fgets ( int handle [, int length] )
?
fgets()函數從handle指定的資源中讀取一行字符。碰到換行符(包含在返回值中)、EOF 或者已經讀取了 length - 1 字節後停止(看先碰到那一種情況);
例:
逐行讀取文件
$handle = fopen("data.txt", "r"); while(!feof($handle)){ $content = fgets($handle); $content= iconv(‘gbk‘,‘utf-8‘,$content); echo $content."<br />”; } fclose($handle);
註意:假設沒有指定 length。則默覺得 1K,或者說 1024 字節。
file()
array file ( string $filename [, int $flags = 0 [, resource $context ]])
file()函數將文件讀取到數組中。各元素由換行符分隔。
例:$arr = file("test.txt"); print_r($arr);
file_get_contents()
string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] )
file_get_contents()函數將文件內容讀到字符串中;
例:$str = file_get_contents("test.txt"); echo $str;
1.3 寫入文件
fwrite()
int fwrite ( resource handle, string string [, int length] )
fwrite()函數將string的內容寫入到由handle指定的資源中。
假設指定length參數。將在寫入Length個字符時停止。
例:$str = "test text"; $fh = fopen("test.txt", "a"); fwrite($fh, $str); fclose($fh);
file_put_contents()
int file_put_contents ( string filename, string data [, int flags [, resource context]] )
file_put_contents()函數將一個字符串寫入文件。與依次調用fopen(),fwrite(),fclose()功能一樣;
例:
$str = "hello"; file_put_contents("test.txt", $str);
1.4 復制,重命名,刪除文件
copy()
bool copy ( string source, string dest )
將文件從 source 復制到 dest。假設成功則返回 TRUE,失敗則返回 FALSE。
例:
Copy("test.txt", "test.txt.bak");
rename()
bool rename ( string oldname, string newname [, resource context] )
嘗試把 oldname 重命名為 newname。 假設成 功則返回 TRUE,失敗則返回 FALSE。
例:
rename("test.txt", “test2.txt”);
unlink()
bool unlink ( string filename )
刪除文件,假設刪除成功返回true, 否則返回false;
例1:
刪除一個文本文件 unlink(“test.txt")。
1.5 讀取文件夾
copy()
bool copy ( string source, string dest )
將文件從 source 復制到 dest。
假設成功則返回 TRUE,失敗則返回 FALSE。
例:
Copy("test.txt", "test.txt.bak");
rename()
bool rename ( string oldname, string newname [, resource context] )
嘗試把 oldname 重命名為 newname。 假設成功則返回 TRUE,失敗則返回 FALSE。
例:
rename("test.txt", “test2.txt”);
unlink()
bool unlink ( string filename )
刪除文件。假設刪除成功返回true, 否則返回false;
例1:
刪除一個文本文件 unlink(“test.txt")。
scandir()
array scandir ( string directory [, int sorting_order [, resource context]] )
返回一個包含有 directory 中的文件和文件夾的數組;
rmdir()
bool rmdir ( string dirname )
刪除文件夾
mkdir()
bool mkdir ( string pathname [, int mode [, bool recursive [, resource context]]] )
?嘗試新建一個由 pathname 指定的文件夾。
1.6 其它文件操作函數
filesize()
int filesize ( string filename )
取得文件的大小,以字節為單位
filectime()
int filectime ( string filename )
取得文件的創建時間,以unix時間戳方式返回
例:
$t = filectime("test.txt"); echo date("Y-m-d H:i:s", $t);
fileatime() 返回文件的最後改變時間;
filemtime() 返回文件的最後改動時間;
註:”最後改變時間”不同於 “最後改動時間”。
最後改變時間指的是對文件inode數據的不論什麽改變。包含改變權限。所屬組。擁有者等; 而最後改動時間指的是對文件內容的改動
file_exists() 檢查文件或文件夾是否存在,假設存在返回true, 否則返回false;
is_readable() 推斷文件是否可讀,假設文件存在而且可讀,則返回true;
is_writable() 推斷文件是否可寫。假設文件存在而且可寫,則返回true;
1.7 解析文件夾路徑函數
basename()
string basename ( string path [, string suffix] )
返回路徑中的文件名稱部份,當指定了可選參數suffix會將這部分內容去掉;
例:
2. 課上練習代碼
<?php
//打開文件
$rh = fopen(‘PHP_3.txt‘, ‘r+‘);
//讀取文件,第一個參數是文件句柄,第二個是讀取方式
//計算文件大小(字節)
$num = filesize(‘PHP_3.txt‘);
$str = fread($rh, $num);
echo $str;
//假設設置文件訪問錯誤,須要去更改文件的權限,屬性 --> 右下角--> 開放權限 --> 改為可讀可寫
echo "<hr>";
//換行讀取 識別 enter 不識別 <br>
$str_1 = fgets($rh);
$str_2 = fgets($rh);
//換行讀取再次讀取還會繼續上次的讀取位置繼續讀取
echo $str_1;
echo "<hr>";
echo $str_2;
//file 將文件內容轉化為數組,<br>直接轉化為換行,回車作為分隔符
$arr = file(‘PHP_3.txt‘);
print_r($arr);
echo "<hr>";
//file_get_contents 讀取文件內容。返回字符串。而且能夠讀取外部網絡數據
// echo file_get_contents(‘PHP_3.txt‘);
//直接讀取站點,存到一個文本中,能夠直接獲取對方的頁面靜態布局。註意,是靜態的!
// $str_3 = file_get_contents(‘http://www.lanou3g.com‘);
// file_put_contents(‘PHP_3.txt‘, $str_3);
//重命名
// rename(‘PHP_3.txt‘, ‘1.txt‘);
// rename(‘1.txt‘,‘PHP_3.txt‘);
//文件拷貝 使用../ 替代上級文件夾
// copy(‘PHP_3.txt‘, ‘../test.txt‘);
//讀取文件夾
//1.打開文件文件夾句柄 .(一個點) 獲取本級文件夾 ..(兩個點)是上級文件夾
$rh_1 = opendir(‘.‘);
// $arr = readdir()
//readdir 獲取文件文件夾,這個和 MySQL 一樣。必須使用循環取出
while ($num = readdir($rh_1)) {
//讀取出來的
echo $num;
echo "<hr>";
}
//讀取文件夾
print_r(scandir(‘.‘));
//創建一個新的文件夾
// mkdir(‘asdasd‘);
//刪除整個文件夾 刪除文件夾必須保證文件夾內部沒有其它文件
// $is_bool = rmdir(‘1‘);
//刪除
// unlink(‘PHP_3.txt‘);
//獲取文件創建時間
echo filectime(‘PHP_3.txt‘);
echo "<hr>";
//返回文件最後訪問的時間
echo fileatime(‘PHP_3.txt‘);
echo "<hr>";
//解析文件詳細名稱
echo basename(‘PHP_3.txt‘,‘txt‘);
echo "<hr>";
//獲取當前文件所在的文件夾的名稱
echo dirname(‘file/PHP_3.txt‘);
echo "<hr>";
//返回全程,拓展名,文件名稱
print_r(pathinfo("PHP_3.txt"));
//改動文件文件夾權限
echo "<hr>";
fclose($rh);
fclose($rh_1);
?>
H5學習_番外篇_PHP數據庫操作