PHP中查找文件的相对路径也许绝对路径的工具
PHP中查找文件的相对路径或者绝对路径的工具
以前面试题目中有一题为求两个文件的相对路径,当时觉得没有电脑,书写代码太麻烦,没有做那个题目,今日工作比较闲遐,就想起这事来,完成了两个函数,代码未经过实际生产环境考验,如引用至生产环境,还请慎重阅读,主要是参考学习
查看源代码打印帮助
1 <?php 2 /** 3 * 文件的相对路径或者绝对路径查找工具 4 * 5 * @author 清源教育<support@tsingyuan.cn > 6 * @version $Id:v 1.0 ,2013/10/13 17:29:00 7 * @copyright (c) Copyright;tsingyuan,2013 8 */ 9 10 /** 11 * 返回文件的绝对路径 12 * 13 * @param string $filename 14 * @return string 15 */ 16 function absoluteroute($filename) 17 { 18 $split = '/\/|\\\/'; 19 $currentdir = preg_split($split, dirname(__FILE__)); 20 $dirarr = preg_split($split, $filename); 21 $diracount = count($dirarr); 22 $cda = count($currentdir); 23 if(strpos(PHP_OS, 'WIN') !== false) 24 $reg = '/\w\:/'; 25 else 26 $reg = '/\//'; 27 if(!preg_match($reg, $dirarr[0])) 28 { 29 foreach($dirarr as $nk=>$name) 30 { 31 if($name == "." || $name == '..') 32 { 33 if($name == '..') 34 $filenamearr = array_slice($currentdir, 0, -($nk+1)); 35 if($name == '.' && $nk == '0') 36 $filenamearr = array_slice($currentdir, 0); 37 } 38 else 39 { 40 $filenamearr[] = $name; 41 } 42 } 43 $filename = implode('/', $filenamearr); 44 } 45 return $filename; 46 } 47 48 /** 49 * 返回两个文件的相对路径 (PS:^_^不错的php学习交流群:276167802,验证:csl,有兴趣的话可以加入进来一起讨论) 50 * (为了保证输入的相对路径参数可用,故此函数依赖absoluteroute) 51 * 52 * @param string $filenamea 53 * @param string $filenameb 54 * @return string 55 */ 56 function relativeroute($filenamea, $filenameb) 57 { 58 $split = '/\/|\\\/'; 59 $filenamea = absoluteroute($filenamea); 60 $filenameb = absoluteroute($filenameb); 61 $dira =preg_split($split, $filenamea); 62 $dirb =preg_split($split, $filenameb); 63 $flag = true; 64 if(count($dira) >= count($dirb)) 65 { 66 $tmp = array(); 67 $tmp = $dira; 68 $dira = $dirb; 69 $dirb = $tmp; 70 $flag = false; 71 } 72 foreach($dira as $k=>$v) 73 if($v != $dirb[$k])break; 74 75 $dirr = array_slice($dirb, $k); 76 77 $k == 1 ? $dtag = '/' : $dtag = './'; 78 $result = $dtag.implode('/', $dirr); 79 if(!$flag) 80 { 81 if($dira[0] == '')array_shift($dira); 82 foreach($dirr as $kk=>&$v) 83 ($kk+1) != count($dirr)? 84 ($v ? 85 $v = '..' : 86 $v = '.' 87 ) : 88 $v = implode('/', array_slice($dira, $k)) ; 89 $result = $dtag.implode('/', $dirr); 90 } 91 return $result; 92 }
本文出自 “清源教育” 博客,转载请注明此处,谢谢!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Alipay PHP...

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
