php目錄操作實(shí)例代碼
<?php
/**
* listdir
*/
header("content-type:text/html;charset=utf-8");
$dirname = "./final/factapplication";
function listdir($dirname) {
$ds = opendir($dirname);
while (false !== ($file = readdir($ds))) {
$path = $dirname.'/'.$file;
if ($file != '.' && $file != '..') {
if (is_dir($path)) {
listdir($path);
} else {
echo $file."<br>";
}
}
}
closedir($ds);
}
listdir($dirname);
核心:遞歸的經(jīng)典應(yīng)用,以及文件和目錄的基本操作。
<?php
/**
* copydir
*/
$srcdir = "../fileupload";
$dstdir = "b";
function copydir($srcdir, $dstdir) {
mkdir($dstdir);
$ds = opendir($srcdir);
while (false !== ($file = readdir($ds))) {
$path = $srcdir."/".$file;
$dstpath = $dstdir."/".$file;
if ($file != "." && $file != "..") {
if (is_dir($path)) {
copydir($path, $dstpath);
} else {
copy($path, $dstpath);
}
}
}
closedir($ds);
}
copydir($srcdir, $dstdir);
核心:copy函數(shù)。
<?php
/**
* deldir
*/
$dirname = 'a';
function deldir($dirname) {
$ds = opendir($dirname);
while (false !== ($file = readdir($ds))) {
$path = $dirname.'/'.$file;
if($file != '.' && $file != '..') {
if (is_dir($path)) {
deldir($path);
} else {
unlink($path);
}
}
}
closedir($ds);
return rmdir($dirname);
}
deldir($dirname);
核心:注意unlink刪除的是帶path的file。
<?php
/**
* dirsize
*/
$dirname = "a";
function dirsize($dirname) {
static $tot;
$ds = opendir($dirname);
while (false !== ($file = readdir($ds))) {
$path = $dirname.'/'.$file;
if ($file != '.' && $file != '..') {
if(is_dir($path)) {
dirsize($path);
} else {
$tot = $tot + filesize($path);
}
}
}
return $tot;
closedir($ds);
}
echo dirsize($dirname);
核心:通過判斷$tot在哪里返回,理解遞歸函數(shù)。
- PHP中文件讀、寫、刪的操作(PHP中對(duì)文件和目錄操作)
- PHP 創(chuàng)建文件(文件夾)以及目錄操作代碼
- php目錄操作函數(shù)之獲取目錄與文件的類型
- PHP常用技術(shù)文之文件操作和目錄操作總結(jié)
- php opendir()列出目錄下所有文件的實(shí)例代碼
- PHP遍歷目錄函數(shù)opendir()、readdir()、closedir()、rewinddir()總結(jié)
- php之readdir函數(shù)用法實(shí)例
- 使用PHP函數(shù)scandir排除特定目錄
- php中目錄操作opendir()、readdir()及scandir()用法示例
相關(guān)文章
基于php設(shè)計(jì)模式中單例模式的應(yīng)用分析
本篇文章是對(duì)php設(shè)計(jì)模式中單例模式的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05PHP 圖片上傳實(shí)現(xiàn)代碼 帶詳細(xì)注釋
PHP 圖片上傳實(shí)現(xiàn)代碼 帶詳細(xì)注釋,喜歡的朋友可以學(xué)習(xí)下。2010-04-04Chart.js在Laravel項(xiàng)目中的應(yīng)用示例
本篇文章主要介紹了Chart.js在Laravel項(xiàng)目中的應(yīng)用示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09PHP實(shí)現(xiàn)權(quán)限管理功能示例
下面小編就為大家?guī)硪黄狿HP實(shí)現(xiàn)權(quán)限管理功能示例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09php結(jié)合GD庫簡單實(shí)現(xiàn)驗(yàn)證碼的示例代碼
這篇文章主要介紹了php結(jié)合GD庫簡單實(shí)現(xiàn)驗(yàn)證碼的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01thinkPHP多域名情況下使用memcache方式共享session數(shù)據(jù)的實(shí)現(xiàn)方法
這篇文章主要介紹了thinkPHP多域名情況下使用memcache方式共享session數(shù)據(jù)的實(shí)現(xiàn)方法,較為詳細(xì)的分析了session的原理及多服務(wù)器共享session的相關(guān)技巧,需要的朋友可以參考下2016-07-07