php目錄操作實例代碼
<?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)典應用,以及文件和目錄的基本操作。
<?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ù)。
相關(guān)文章
linux實現(xiàn)php定時執(zhí)行cron任務(wù)詳解
linux實現(xiàn)php定時執(zhí)行cron任務(wù)2013-12-12
PHP autoload與spl_autoload自動加載機制的深入理解
本篇文章是對PHP中的autoload與spl_autoload自動加載機制進行了詳細的分析介紹,需要的朋友參考下2013-06-06
WordPress上傳圖片錯誤:不是合法的JSON響應解決辦法
這篇文章主要給大家介紹了關(guān)于WordPress上傳圖片錯誤:不是合法的JSON響應的解決辦法,WordPress提示JSON錯誤通常是由于服務(wù)器配置或插件沖突引起的,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-08-08
php的sprintf函數(shù)的用法 控制浮點數(shù)格式
這篇文章主要介紹了php的sprintf函數(shù)的用法,需要的朋友可以參考下2014-02-02
WordPress特定文章對搜索引擎隱藏或只允許搜索引擎查看
這篇文章主要介紹了WordPress特定文章對搜索引擎隱藏或只允許搜索引擎查看的方法,可以根據(jù)SEO的需要來進行調(diào)整,需要的朋友可以參考下2015-12-12

