search
Homephp教程php手册Relative path to network absolute path (supports second-level domain names) ./a.jpg => http://

When I was writing about using Thunder to download yesterday, I found that the ./a.jpg file converted to Thunder was not the network address, so I wrote a method to convert it. I am currently at the basic level of PHP, so I would like to ask for help if I have any deficiencies. It will automatically determine the current domain name and main domain name. The following is the form: ./a.jpg = http://about.w.com/a.jpg; ../a.jpg =http://www.w.com /a.jpg None/*

Yesterday, when I was writing about using Thunder to download, I found that the ./a.jpg file converted to Thunder was not the network address, so I wrote a method to convert it. I am currently at the basic level of PHP, so I would like to ask for help if I have any deficiencies.
It will automatically determine the current domain name and main domain name. The following is the form:
./a.jpg => http://about.w.com/a.jpg;
../a.jpg => http: //www.w.com/a.jpg
/**
 * 相对路径转网络绝对路径
 * @param string $file
 * @return string
 */
function dirToHttpUrl($file) {

	//判断文件是否存在
	if (!file_exists($file)) {
		return false;
	}

	//域名
	$nowUrl = dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);	//当前域名
	$tempUrl = explode('.', $_SERVER['HTTP_HOST']);
	$dirUrl = 'http://www.'.$tempUrl[1].'.'.$tempUrl[2].'/';					//主域名

	//文件路径的层次统计
	$tempFile = explode('../', $file);
	$tempNum = array_count_values($tempFile);

	if (array_key_exists('', $tempNum)) {
		$fileNum = $tempNum[''];
		$fileEnd = end($tempFile);
	} else {
		$fileNum = 0;
		$fileEnd = '/'.substr($tempFile[0], 2);
	}

	//域名层次统计
	$tempWeb = explode('/', $nowUrl);
	$tempWeb = array_slice($tempWeb, 3);
	$webNum = count($tempWeb);

	//文件对应的域名
	if ($fileNum > $webNum) {
		$nowUrl = $dirUrl;
	}

	//返回
	return $nowUrl.$fileEnd;

}

//dirToHttpUrl('./1.jpg');
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment