Maison > php教程 > php手册 > le corps du texte

PHP文件操作之获取目录下文件与计算相对路径的方法,

WBOY
Libérer: 2016-06-13 08:47:55
original
1106 Les gens l'ont consulté

PHP文件操作之获取目录下文件与计算相对路径的方法,

获取目录下文件

1、获取目录下文件,不包括子目录

//获取某目录下所有文件、目录名(不包括子目录下文件、目录名) 
  $handler = opendir($dir); 
  while (($filename = readdir($handler)) !== false) {//务必使用!==,防止目录下出现类似文件名“0”等情况 
    if ($filename != "." && $filename != "..") { 
        $files[] = $filename ; 
      } 
    } 
  } 
  closedir($handler); 
    
//打印所有文件名 
foreach ($filens as $value) { 
  echo $value."<br />"; 
} 

Copier après la connexion

2、获取目录下所有文件,包括子目录

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 />"; 
} 

Copier après la connexion

计算两个文件之间的相对路径方法
php 计算两个文件之间的相对路径方法

例如:
文件A 的路径是 /home/web/lib/img/cache.php
文件B的路径是 /home/web/api/img/show.php
那么,文件A相对于文件B的路径是 ../../lib/img/cache.php,即文件B 访问 文件A的相对路径。

function getRelativePath

<&#63;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; 
} 
&#63;> 

Copier après la connexion

demo

<&#63;php 
$path1 = '/home/web/lib/img/cache.php'; 
$path2 = '/home/web/api/img/show.php'; 
 
echo getRelativePath($path1, $path2); // ../../lib/img/cache.php 
&#63;> 
Copier après la connexion

您可能感兴趣的文章:

  • php从完整文件路径中分离文件目录和文件名的方法
  • PHP循环输出指定目录下的所有文件和文件夹路径例子(简单实用)
  • PHP获取文件绝对路径的代码(上一级目录)
  • php中检查文件或目录是否存在的代码小结
  • PHP中文件读、写、删的操作(PHP中对文件和目录操作)
  • PHP获取当前文件所在目录 getcwd()函数
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!