PHP 創(chuàng)建文件(文件夾)以及目錄操作代碼
更新時(shí)間:2010年03月04日 19:06:16 作者:
PHP 創(chuàng)建文件(文件夾)以及目錄操作代碼,需要的朋友可以參考下。
一、目錄操作
首先是從目錄讀取的函數(shù),opendir(),readdir(),closedir(),使用的時(shí)候是先打開文件句柄,而后迭代列出:
<?php
$base_dir="filelist/";
$fso=opendir($base_dir);
echo $base_dir."<hr/>";
while($flist=readdir($fso)){
echo $flist."<br/>";
}
closedir($fso)
?>
這是講返回文件目錄下面的文件已經(jīng)目錄的程序(0文件將返回false).
有時(shí)候需要知道目錄的信息,可以使用dirname($path)和basename($path),分別返回路徑的目錄部分和文件名名稱部分,可用disk_free_space($path)返回查看空間剩余空間.
創(chuàng)建命令:
mkdir($path,0777):0777是權(quán)限碼,在非window下可用umask()函數(shù)設(shè)置.
rmdir($path):將刪除路徑在$path的文件.
二、文件操作
● 新建文件
首先,確定你所要新建文件所在的目錄權(quán)限; 建議設(shè)備為777。然后,新建文件的名稱建議使用絕對(duì)路徑。
<?php
$filename="test.txt";
$fp=fopen("$filename", "w+"); //打開文件指針,創(chuàng)建文件
if ( !is_writable($filename) ){
die("文件:" .$filename. "不可寫,請(qǐng)檢查!");
}
//fwrite($filename, "anything you want to write to $filename.";
fclose($fp); //關(guān)閉指針
● 讀文件
首先是一個(gè)文件看能不能讀取(權(quán)限問題),或者存在不,我們可以用is_readable函數(shù)獲取信息.:
<?php
$file = 'dirlist.php';
if (is_readable($file) == false) {
die('文件不存在或者無法讀取');
} else {
echo '存在';
}
?>
判斷文件存在的函數(shù)還有file_exists(下面演示),但是這個(gè)顯然無is_readable全面.,當(dāng)一個(gè)文件存在的話可以用
<?php
$file = "filelist.php";
if (file_exists($file) == false) {
die('文件不存在');
}
$data = file_get_contents($file);
echo htmlentities($data);
?>
但是file_get_contents函數(shù)在較低版本上不支持,可以先創(chuàng)建文件的一個(gè)句柄,然后用指針讀取全部:
還有一種方式,可以讀取二進(jìn)制的文件:
$data = implode('', file($file));
● 寫文件
和讀取文件的方式一樣,先看看是不是能寫:
<?php
$file = 'dirlist.php';
if (is_writable($file) == false) {
die("You have no right to write!");
}
?>
能寫了的話可以使用file_put_contents函數(shù)寫入:
<?php
$file = 'dirlist.php';
if (is_writable($file) == false) {
die('我是雞毛,我不能');
}
$data = '我是可鄙,我想要';
file_put_contents ($file, $data);
?>
file_put_contents函數(shù)在php5中新引進(jìn)的函數(shù)(不知道存在的話用function_exists函數(shù)先判斷一下)低版本的php無法使用,可以使用如下方式:
$f = fopen($file, 'w');
fwrite($f, $data);
fclose($f);
替換之.
寫文件的時(shí)候有時(shí)候需要鎖定,然后寫:
function cache_page($pageurl,$pagedata){
if(!$fso=fopen($pageurl,'w')){
$this->warns('無法打開緩存文件.');//trigger_error
return false;
}
if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型鎖定
$this->warns('無法鎖定緩存文件.');//trigger_error
return false;
}
if(!fwrite($fso,$pagedata)){//寫入字節(jié)流,serialize寫入其他格式
$this->warns('無法寫入緩存文件.');//trigger_error
return false;
}
flock($fso,LOCK_UN);//釋放鎖定
fclose($fso);
return true;
}
● 復(fù)制,刪除文件
php刪除文件非常easy,用unlink函數(shù)簡單操作:
<?php
$file = 'dirlist.php';
$result = @unlink ($file);
if ($result == false) {
echo '蚊子趕走了';
} else {
echo '無法趕走';
}
?>
即可.
復(fù)制文件也很容易:
<?php
$file = 'yang.txt';
$newfile = 'ji.txt'; # 這個(gè)文件父文件夾必須能寫
if (file_exists($file) == false) {
die ('小樣沒上線,無法復(fù)制');
}
$result = copy($file, $newfile);
if ($result == false) {
echo '復(fù)制記憶ok';
}
?>
可以使用rename()函數(shù)重命名一個(gè)文件夾.其他操作都是這幾個(gè)函數(shù)組合一下就能實(shí)現(xiàn)的.
● 獲取文件屬性
我說幾個(gè)常見的函數(shù):
獲取最近修改時(shí)間:
<?php
$file = 'test.txt';
echo date('r', filemtime($file));
?>
返回的說unix的時(shí)間戳,這在緩存技術(shù)常用.
相關(guān)的還有獲取上次被訪問的時(shí)間fileatime(),filectime()當(dāng)文件的權(quán)限,所有者,所有組或其它 inode 中的元數(shù)據(jù)被更新時(shí)間,fileowner()函數(shù)返回文件所有者
$owner = posix_getpwuid(fileowner($file));
(非window系統(tǒng)),ileperms()獲取文件的權(quán)限,
<?php
$file = 'dirlist.php';
$perms = substr(sprintf('%o', fileperms($file)), -4);
echo $perms;
?>
filesize()返回文件大小的字節(jié)數(shù):
<?php
// 輸出類似:somefile.txt: 1024 bytes
$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';
?>
獲取文件的全部信息有個(gè)返回?cái)?shù)組的函數(shù)stat()函數(shù):
<?php
$file = 'dirlist.php';
$perms = stat($file);
var_dump($perms);
?>
首先是從目錄讀取的函數(shù),opendir(),readdir(),closedir(),使用的時(shí)候是先打開文件句柄,而后迭代列出:
復(fù)制代碼 代碼如下:
<?php
$base_dir="filelist/";
$fso=opendir($base_dir);
echo $base_dir."<hr/>";
while($flist=readdir($fso)){
echo $flist."<br/>";
}
closedir($fso)
?>
這是講返回文件目錄下面的文件已經(jīng)目錄的程序(0文件將返回false).
有時(shí)候需要知道目錄的信息,可以使用dirname($path)和basename($path),分別返回路徑的目錄部分和文件名名稱部分,可用disk_free_space($path)返回查看空間剩余空間.
創(chuàng)建命令:
mkdir($path,0777):0777是權(quán)限碼,在非window下可用umask()函數(shù)設(shè)置.
rmdir($path):將刪除路徑在$path的文件.
二、文件操作
● 新建文件
首先,確定你所要新建文件所在的目錄權(quán)限; 建議設(shè)備為777。然后,新建文件的名稱建議使用絕對(duì)路徑。
復(fù)制代碼 代碼如下:
<?php
$filename="test.txt";
$fp=fopen("$filename", "w+"); //打開文件指針,創(chuàng)建文件
if ( !is_writable($filename) ){
die("文件:" .$filename. "不可寫,請(qǐng)檢查!");
}
//fwrite($filename, "anything you want to write to $filename.";
fclose($fp); //關(guān)閉指針
● 讀文件
首先是一個(gè)文件看能不能讀取(權(quán)限問題),或者存在不,我們可以用is_readable函數(shù)獲取信息.:
復(fù)制代碼 代碼如下:
<?php
$file = 'dirlist.php';
if (is_readable($file) == false) {
die('文件不存在或者無法讀取');
} else {
echo '存在';
}
?>
判斷文件存在的函數(shù)還有file_exists(下面演示),但是這個(gè)顯然無is_readable全面.,當(dāng)一個(gè)文件存在的話可以用
復(fù)制代碼 代碼如下:
<?php
$file = "filelist.php";
if (file_exists($file) == false) {
die('文件不存在');
}
$data = file_get_contents($file);
echo htmlentities($data);
?>
但是file_get_contents函數(shù)在較低版本上不支持,可以先創(chuàng)建文件的一個(gè)句柄,然后用指針讀取全部:
還有一種方式,可以讀取二進(jìn)制的文件:
復(fù)制代碼 代碼如下:
$data = implode('', file($file));
● 寫文件
和讀取文件的方式一樣,先看看是不是能寫:
復(fù)制代碼 代碼如下:
<?php
$file = 'dirlist.php';
if (is_writable($file) == false) {
die("You have no right to write!");
}
?>
能寫了的話可以使用file_put_contents函數(shù)寫入:
復(fù)制代碼 代碼如下:
<?php
$file = 'dirlist.php';
if (is_writable($file) == false) {
die('我是雞毛,我不能');
}
$data = '我是可鄙,我想要';
file_put_contents ($file, $data);
?>
file_put_contents函數(shù)在php5中新引進(jìn)的函數(shù)(不知道存在的話用function_exists函數(shù)先判斷一下)低版本的php無法使用,可以使用如下方式:
復(fù)制代碼 代碼如下:
$f = fopen($file, 'w');
fwrite($f, $data);
fclose($f);
替換之.
寫文件的時(shí)候有時(shí)候需要鎖定,然后寫:
復(fù)制代碼 代碼如下:
function cache_page($pageurl,$pagedata){
if(!$fso=fopen($pageurl,'w')){
$this->warns('無法打開緩存文件.');//trigger_error
return false;
}
if(!flock($fso,LOCK_EX)){//LOCK_NB,排它型鎖定
$this->warns('無法鎖定緩存文件.');//trigger_error
return false;
}
if(!fwrite($fso,$pagedata)){//寫入字節(jié)流,serialize寫入其他格式
$this->warns('無法寫入緩存文件.');//trigger_error
return false;
}
flock($fso,LOCK_UN);//釋放鎖定
fclose($fso);
return true;
}
● 復(fù)制,刪除文件
php刪除文件非常easy,用unlink函數(shù)簡單操作:
復(fù)制代碼 代碼如下:
<?php
$file = 'dirlist.php';
$result = @unlink ($file);
if ($result == false) {
echo '蚊子趕走了';
} else {
echo '無法趕走';
}
?>
即可.
復(fù)制文件也很容易:
復(fù)制代碼 代碼如下:
<?php
$file = 'yang.txt';
$newfile = 'ji.txt'; # 這個(gè)文件父文件夾必須能寫
if (file_exists($file) == false) {
die ('小樣沒上線,無法復(fù)制');
}
$result = copy($file, $newfile);
if ($result == false) {
echo '復(fù)制記憶ok';
}
?>
可以使用rename()函數(shù)重命名一個(gè)文件夾.其他操作都是這幾個(gè)函數(shù)組合一下就能實(shí)現(xiàn)的.
● 獲取文件屬性
我說幾個(gè)常見的函數(shù):
獲取最近修改時(shí)間:
復(fù)制代碼 代碼如下:
<?php
$file = 'test.txt';
echo date('r', filemtime($file));
?>
返回的說unix的時(shí)間戳,這在緩存技術(shù)常用.
相關(guān)的還有獲取上次被訪問的時(shí)間fileatime(),filectime()當(dāng)文件的權(quán)限,所有者,所有組或其它 inode 中的元數(shù)據(jù)被更新時(shí)間,fileowner()函數(shù)返回文件所有者
$owner = posix_getpwuid(fileowner($file));
(非window系統(tǒng)),ileperms()獲取文件的權(quán)限,
復(fù)制代碼 代碼如下:
<?php
$file = 'dirlist.php';
$perms = substr(sprintf('%o', fileperms($file)), -4);
echo $perms;
?>
filesize()返回文件大小的字節(jié)數(shù):
復(fù)制代碼 代碼如下:
<?php
// 輸出類似:somefile.txt: 1024 bytes
$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';
?>
獲取文件的全部信息有個(gè)返回?cái)?shù)組的函數(shù)stat()函數(shù):
復(fù)制代碼 代碼如下:
<?php
$file = 'dirlist.php';
$perms = stat($file);
var_dump($perms);
?>
您可能感興趣的文章:
- php中的filesystem文件系統(tǒng)函數(shù)介紹及使用示例
- PHP 文件系統(tǒng)詳解
- PHP文件系統(tǒng)管理(實(shí)例講解)
- php文件系統(tǒng)處理方法小結(jié)
- PHP開發(fā)文件系統(tǒng)實(shí)例講解
- php讀取目錄及子目錄下所有文件名的方法
- PHP中文件讀、寫、刪的操作(PHP中對(duì)文件和目錄操作)
- php遍歷目錄與文件夾的多種方法詳解
- PHP判斷文件是否存在、是否可讀、目錄是否存在的代碼
- php中檢查文件或目錄是否存在的代碼小結(jié)
- PHP獲取當(dāng)前文件的父目錄方法匯總
- PHP FileSystem 文件系統(tǒng)常用api整理總結(jié)
相關(guān)文章
PHP Memcached應(yīng)用實(shí)現(xiàn)代碼
在很多場合,我們都會(huì)聽到 memcached 這個(gè)名字,但很多同學(xué)只是聽過,并沒有用過或?qū)嶋H了解過,只知道它是一個(gè)很不錯(cuò)的東東。這里簡單介紹一下,memcached 是高效、快速的分布式內(nèi)存對(duì)象緩存系統(tǒng),主要用于加速 WEB 動(dòng)態(tài)應(yīng)用程序。2010-02-02PHP編程獲取各個(gè)時(shí)間段具體時(shí)間的方法
這篇文章主要介紹了PHP編程獲取各個(gè)時(shí)間段具體時(shí)間的方法,結(jié)合實(shí)例形式分析了基于date與strtotime函數(shù)進(jìn)行日期時(shí)間運(yùn)算的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05說說PHP的autoLoad自動(dòng)加載機(jī)制
php的autoload大致可以使用兩種方法:__autoload和spl方法。這兩種方法又各有不同的幾種使用方法2012-09-09解析PHP 使用curl提交json格式數(shù)據(jù)
本篇文章是對(duì)PHP中使用curl提交json格式數(shù)據(jù)的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP操作FTP類 (上傳、下載、移動(dòng)、創(chuàng)建等)
這篇文章主要介紹了PHP操作FTP類,實(shí)現(xiàn)FTP上傳、FTP下載、FTP移動(dòng)、FTP創(chuàng)建等,感興趣的小伙伴們可以參考一下2016-03-03