//获取某目录下所有文件、目录名(不包括子目录下文件、目录名)
$handler = opendir($dir);
while (($filename = readdir($handler)) !== false) {//务必使用!==,防止目录下出现类似文件名“0”等情况
if ($filename != "." && $filename != "..") {
$files[] = $filename ;
}
}
}
closedir($handler);
//打印所有文件名
foreach ($filens as $value) {
echo $value."<br />";
}
function get_allfiles($path,&$files) {
if(is_dir($path)){
$dp = dir($path);
while ($file = $dp ->read()){
if($file !="." && $file !=".."){
get_allfiles($path."/".$file, $files);
}
}
$dp ->close();
}
if(is_file($path)){
$files[] = $path;
}
}
function get_filenamesbydir($dir){
$files = array();
get_allfiles($dir,$files);
return $files;
}
$filenames = get_filenamesbydir("static/image/");
//打印所有文件名,包括路径
foreach ($filenames as $value) {
echo $value."<br />";
}
<?php
/** 计算path1 相对于 path2 的路径,即在path2引用paht1的相对路径
* @param String $path1
* @param String $path2
* @return String
*/
function getRelativePath($path1, $path2){
$arr1 = explode('/', $path1);
$arr2 = explode('/', $path2);
// 获取相同路径的部分
$intersection = array_intersect_assoc($arr1, $arr2);
$depth = 0;
for($i=0,$len=count($intersection); $i<$len; $i++){
if(!isset($intersection[$i])){
$depth = $i;
break;
}
}
// 将path2的/ 转为 ../,path1获取后面的部分,然后合拼
$tmp = array_merge(array_fill(0, count($arr2)-$depth-1, '..'), array_slice($arr1, $depth));
$relativePath = implode('/', $tmp);
return $relativePath;
}
?>
<?php $path1 = '/home/web/lib/img/cache.php'; $path2 = '/home/web/api/img/show.php'; echo getRelativePath($path1, $path2); // ../../lib/img/cache.php ?>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有