本文目錄一覽:
php調取圖片的相對路徑
沒有問題,相對路徑一般是相對你項目跟目錄 或者根目錄下的制定目錄
define(“ROOT”,dirname(__FILE__).”/”);//這個代碼出現在根目錄文件中
define(“IMG”,ROOT.”img/”);
按照你的假設
的ROOT是d:\www\
你可以定義你的IMG為d:\www\img\
這樣你存儲導數據庫中就直接存a.jpg 調用的地方 你直接就 IMG.$img_path.($img_path就是你從數據庫中讀取的相對路徑)
php 下面的代碼是什麼意思
不知道你具體想問什麼。我幫你解釋一下這個算法吧,我看你以前問過很多問題了,相信單獨每句話都應該能明白。
這段代碼是求文件b相對於文件a的路徑,basename($b)是用來獲得文件的文件名的,dirname($b)是用來獲取文件所在路徑的。
算法中先獲取兩個文件的路徑,切割為數組之後已目錄較深的那個數組為基準比較數組中的每一個元素,然後根據比較結果添加../或者目錄名。最後獲得一個相對路徑。
從if($path1[$i] != $path2[$i] isset($path1[$i])){ 這行就看出代碼質量很差,不做過多說明了。
兩邊的表達式順序不對,這是很菜鳥的錯誤。
我提供給你一個吧,不過這個是計算兩個目錄之間的相對路徑的,如果需要計算文件間的相對路徑你可以模仿你提供的那個代碼用dirname和basename做個附加處理就行了。
/**
* Calculate relative path
* @param string $basePath
* @param string $targetPath
* @return string
*/
function CalculateRelativePath($basePath, $targetPath) {
$basePath = rtrim(str_replace(‘\\’, ‘/’, $basePath), ‘/’);
$targetPath = rtrim(str_replace(‘\\’, ‘/’, $targetPath), ‘/’);
$_targetPath = $targetPath;
if ($basePath == $targetPath) {
return ‘.’;
}
$basePath = explode(‘/’, $basePath);
$targetPath = explode(‘/’, $targetPath);
$length = count($basePath);
if (count($targetPath) $length) {
$length = count($targetPath);
}
$basePath[0] = strtoupper($basePath[0]);
$targetPath[0] = strtoupper($targetPath[0]);
if ($basePath[0] != $targetPath[0]) {
return $_targetPath;
}
$relativePath = ”;
$i = 0;
for(; $i $length; $i ++) {
if (! isset($basePath[$i])) {
$base = false;
} else {
$base = $basePath[$i];
}
if (! isset($targetPath[$i])) {
$target = false;
} else {
$target = $targetPath[$i];
}
/* Ignore case if windows */
if (! empty($basePath[0])) {
$target = strtoupper($target);
$base = strtoupper($base);
}
if ($base !== $target) {
break;
}
}
$length = count($basePath);
for ($j = $i; $j $length; $j ++) {
$relativePath .= ‘../’;
}
$length = count($targetPath);
for ($j = $i; $j count($targetPath); $j ++) {
$relativePath .= $targetPath[$j] . ‘/’;
}
$relativePath = rtrim($relativePath, ‘/’);
if (empty($relativePath)) {
$relativePath = ‘.’;
}
return $relativePath;
}
如果對你有幫助的話,希望你能把題目修改為:如何使用PHP計算兩個目錄之間的相對路徑,這樣我就能拿去申請優質回答咯,非常感謝。
php相對路徑要怎麼寫?
read.php中相應路徑是這樣的:
require_once(./order/aaa.txt);
aaa.txt就是你在order目錄下的記事本文件,./表示當前目錄,../表示上級目錄
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/250599.html