php關於檔案的建立和修改

來源:互聯網
上載者:User

標籤:style   io   ar   os   使用   sp   檔案   on   cti   

1.目錄的操作

 

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

 

<?php

$base_dir=”filelist/” ;    //定義需要操作的檔案

$fso=opendir($base_dir) ;  //使用opendir開啟對應路徑,並返回對應的資源類型

Echo $base_dir.”<br />”;   //進行輸出查看

While($flist=readdir($fso))

{

   Echo $flist.”<br />”

}

Closedir($fso);     //防止記憶體的溢出,最後需要進行關閉

?>

 

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

 

建立檔案夾命令:

 

Mkdir($path,0777):     0777是檔案建立時候的許可權,這個命令僅僅適用於unix系統

 

Rmdir($path);         刪除路徑為$path的檔案

 

2.關於檔案的操作

 

2.1 建立檔案

  

首先。要求確定你所要建立檔案所在目錄許可權,建議設定為0777.然後,建立檔案的名稱建議適用絕對路徑

 

<?php

$filename=”text.txt”;

 

$fp=fopen(“$filename”,”w+”);

 

If(!is_writable($filename))

{

   Die(“檔案:”.filename.”不可寫,請檢查!”);

}

 

Fwrite($filename,”anything you want to write to $filename”);

 

Fclose($fp);  //關閉記憶體的暫用

 

?>

 

2.2讀取檔案

 

  首先要確定檔案是否擁有讀取的許可權(或則讀取的檔案是否存在),我們可以用is_readable函數擷取資訊;

 

<?php

 

$file=”dirlist.php”;

If(is_readable($file)==false)

{

    Die(“檔案不存在或則無法讀取”);

}else{

    Echo “存在”;

}

 

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

<?php

  $file=”filelist.php”;

If(file_exists($file)==false)

{

  Die(“檔案不存在”);

}

$data=file_get_contents($file);

Echo htmllentities($data);             //需要測試這個函數是什麼意思;

還有一種方式,可以讀取二進位檔案

 

$data=inplode(“”,file($file));         //需要測試

 

?>

 

2.3寫檔案

 

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

<?php

  $file=”dirlist.php”;

  If(is_writable($file)==false) 

  {

     Die(“檔案沒有寫入的許可權”);

 

   }

?>

 

能夠寫入的話使用的是file_put_contents函數寫入:

<?

  $file=”dirlist.php”;

  If(is_writable($file)==false)

 {

   Die(“我是雞毛,我不能。”);

 }

 $date=”我是寫入內容”;

 File_put_contents($file,$data);

?>

 

File_put_contents函數是在php5中新引進的函數(不知道存在的話用

Function_exists函數先判斷一下),低版本的php無法使用,可以使用如下的方式:

 

$f=fopen($file,”w”);

Fwrite($f,$data);

Fclose($f);

 

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

Function cache_page($pageurl,$pagedata)

{

   If(!$fso=fopen($pageurl,”w”)) 

   {

      $this->warns(“無法開啟快取檔案。”); //錯誤提醒

      Return false;

   }

   If(!loack($fos,LOCK_EX))

{

   //lock_nb ,排它型鎖定

   $this->warns(“無法鎖定快取檔案。”);//錯誤提醒

   Return false

}

If(!fwrite($fso,$pagedata))

{

   //寫入位元組流,$serialize寫入其他的格式

  $this->warns(“無法寫入快取檔案”); //錯誤提醒

  Return false;

}

Flock($fso,LOCK_UN);//解放鎖定

Fclose($fso);

Return true ;

}

 

 

2.4複製,刪除檔案

 

Php刪除檔案是非常簡單的,用unlink函數簡單操作:

<?php

  $file=”dirlist.php”;

  [email protected]($file);

  If($result==false)

  {

    Echo “檔案已經刪除”;

  }else{

     Echo “檔案刪除失敗”;

  }

?>

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

 

2.5擷取檔案屬性

 

我這個說幾個常見的幻術

 

擷取最近修改時間

<?php

  $file=”text.txt”;

Echo date(“r’,filemtime($file));

?>

 

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

 

相關的還有擷取上次被訪問的時間fileatime(),filectime()當前檔案的許可權,所有則,所有組或其他inode中的元素被更新時間,fileowner()函數返迴文件所有者;

 

$owner=posix_getpuid(flieowner($file));

 

(非window系統),ileperms 擷取檔案的許可權,

<?php

  $file=”dirlist.php”;

$perms=substr(sprintf(‘%o’,fileperms($file)),-4);

Echo $perms;

?>

.

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

 

 

 

<?php

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

 

   $filename=”somefile.txt”;

   Echo $filename.”:”.filesize($filename).”bytes”;

?>

 

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

<?php

   $file=”dirlist.php”;

$perms=stat($file);

Var_dump($perms);

?>

 

 

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.