php目錄與檔案操作的執行個體教程

來源:互聯網
上載者:User

一、目錄操作首先是從目錄讀取的函數,opendir(),readdir(),closedir(),使用的時候是先開啟檔案控制代碼,而後迭代列出:

  1. $base_dir="filelist/";
  2. $fso=opendir($base_dir);
  3. echo $base_dir."";
  4. while($flist=readdir($fso)){
  5. echo $flist."
    ";
  6. }
  7. closedir($fso)
  8. ?>
複製代碼

這是講返迴文件目錄下面的檔案已經目錄的程式(0檔案將返回false).

有時候需要知道目錄的資訊,可以使用dirname($path)和basename($path),分別返迴路徑的目錄部分和檔案名稱名稱部分,可用disk_free_space($path)返回查看空間剩餘空間.

建立命令:mkdir($path,0777):0777是許可權碼,在非window下可用umask()函數設定.rmdir($path):將刪除路徑在$path的檔案.

二、檔案操作

建立檔案首先,確定你所要建立檔案所在的目錄許可權; 建議裝置為777。然後,建立檔案的名稱建議使用絕對路徑。

  1. $filename="test.txt";
  2. $fp=fopen("$filename", "w+"); //開啟檔案指標,建立檔案
  3. if ( !is_writable($filename) ){
  4. die("檔案:" .$filename. "不可寫,請檢查!");
  5. }
  6. //fwrite($filename, "anything you want to write to $filename.";
  7. fclose($fp); //關閉指標
複製代碼

讀檔案 首先是一個檔案看能不能讀取(許可權問題),或者存在不,我們可以用is_readable函數擷取資訊.:

  1. $file = 'dirlist.php';
  2. if (is_readable($file) == false) {
  3. die('檔案不存在或者無法讀取');
  4. } else {
  5. echo '存在';
  6. }
  7. ?>
複製代碼

判斷檔案存在的函數還有file_exists(下面示範),但是這個顯然無is_readable全面.,當一個檔案存在的話可以用

  1. $file = "filelist.php";
  2. if (file_exists($file) == false) {
  3. die('檔案不存在');
  4. }
  5. $data = file_get_contents($file);
  6. echo htmlentities($data);
  7. ?>
複製代碼

但是file_get_contents函數在較低版本上不支援,可以先建立檔案的一個控制代碼,然後用指標讀取全部:

還有一種方式,可以讀取二進位的檔案:$data = implode('', file($file));

寫檔案 和讀取檔案的方式一樣,先看看是不是能寫:

  1. $file = 'dirlist.php';
  2. if (is_writable($file) == false) {
  3. die("You have no right to write!");
  4. }
  5. ?>
複製代碼

能寫了的話可以使用file_put_contents函數寫入:

  1. $file = 'dirlist.php';
  2. if (is_writable($file) == false) {
  3. die('我是雞毛,我不能');
  4. }
  5. $data = '我是可鄙,我想要';
  6. file_put_contents ($file, $data);
  7. ?>
複製代碼

file_put_contents函數在php5中新引進的函數(不知道存在的話用function_exists函數先判斷一下)低版本的php無法使用,可以使用如下方式:

  1. $f = fopen($file, 'w');
  2. fwrite($f, $data);
  3. fclose($f);
複製代碼

替換之。

寫檔案的時候有時候需要鎖定,然後寫:

  1. function cache_page($pageurl,$pagedata){
  2. if(!$fso=fopen($pageurl,'w')){
  3. $this->warns('無法開啟快取檔案.');//trigger_error
  4. return false;
  5. }
  6. if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型鎖定
  7. $this->warns('無法鎖定快取檔案.');//trigger_error
  8. return false;
  9. }
  10. if(!fwrite($fso,$pagedata)){//寫入位元組流,serialize寫入其他格式
  11. $this->warns('無法寫入快取檔案.');//trigger_error
  12. return false;
  13. }
  14. flock($fso,LOCK_UN);//釋放鎖定
  15. fclose($fso);
  16. return true;
  17. }
複製代碼

複製,刪除檔案 php刪除檔案非常easy,用unlink函數簡單操作:

  1. $file = 'dirlist.php';
  2. $result = @unlink ($file);
  3. if ($result == false) {
  4. echo '蚊子趕走了';
  5. } else {
  6. echo '無法趕走';
  7. }
  8. ?>
複製代碼

即可。

複製檔案:

  1. $file = 'yang.txt';
  2. $newfile = 'ji.txt'; # 這個檔案父資料夾必須能寫
  3. if (file_exists($file) == false) {
  4. die ('小樣沒上線,無法複製');
  5. }
  6. $result = copy($file, $newfile);
  7. if ($result == false) {
  8. echo '複製記憶ok';
  9. }
  10. ?>
複製代碼

可以使用rename()函數重新命名一個檔案夾.其他動作都是這幾個函數組合一下就能實現的。

擷取檔案屬性

我說幾個常見的函數:擷取最近修改時間:

  1. $file = 'test.txt';
  2. echo date('r', filemtime($file));
  3. ?>
複製代碼

返回的說unix的時間戳記,這在緩衝技術常用。

相關的還有擷取上次被訪問的時間fileatime(),filectime()當檔案的許可權,所有者,所有組或其它 inode 中的中繼資料被更新時間,fileowner()函數返迴文件所有者$owner = posix_getpwuid(fileowner($file));(非window系統),ileperms()擷取檔案的許可權,

  1. $file = 'dirlist.php';
  2. $perms = substr(sprintf('%o', fileperms($file)), -4);
  3. echo $perms;
  4. ?>
複製代碼

filesize()返迴文件大小的位元組數:

  1. // 輸出類似:somefile.txt: 1024 bytes

  2. $filename = 'somefile.txt';

  3. echo $filename . ': ' . filesize($filename) . ' bytes';
  4. ?>

複製代碼

擷取檔案的全部資訊有個返回數組的函數stat()函數:

  1. $file = 'dirlist.php';
  2. $perms = stat($file);
  3. var_dump($perms);
  4. ?>
複製代碼

您可能感興趣的文章:php檔案操作類的代碼一例php檔案操作的小例子一個不錯的文字檔操作的php類PHP檔案操作方法問答php中目錄與檔案操作詳解

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.