Home > Backend Development > PHP Tutorial > PHP file operation: methods to obtain files in a directory and calculate relative paths, _PHP tutorial

PHP file operation: methods to obtain files in a directory and calculate relative paths, _PHP tutorial

WBOY
Release: 2016-07-12 09:01:08
Original
981 people have browsed it

PHP file operation method to obtain files in a directory and calculate relative paths,

Get files in a directory

1. Get the files in the directory, excluding subdirectories

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

Copy after login

2. Get all files in the directory, including subdirectories

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

Copy after login

Method to calculate the relative path between two files
php method to calculate the relative path between two files

For example:
The path of file A is /home/web/lib/img/cache.php
The path of file B is /home/web/api/img/show.php
Then, the path of file A relative to file B is ../../lib/img/cache.php, that is, the relative path of file B accessing file 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;> 

Copy after login

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;> 
Copy after login

Articles you may be interested in:

  • How to separate file directories and file names from full file paths in PHP
  • PHP loops to output all files and files in the specified directory Example of folder path (simple and practical)
  • PHP code to obtain the absolute path of a file (upper level directory)
  • Summary of the code to check whether a file or directory exists in PHP
  • PHP File reading, writing, and deletion operations (file and directory operations in PHP)
  • PHP gets the directory where the current file is located getcwd() function

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1089944.htmlTechArticlePHP file operation method of obtaining files in the directory and calculating relative paths, obtaining files in the directory 1. Obtaining the directory Files, excluding subdirectories //Get all files and directories under a directory...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template