Relative path to network absolute path (supports second-level domain name) ./a.jpg => http://www.w.com/a.jpg
Release: 2016-07-25 08:48:58
Original
1536 people have browsed it
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
- /**
- * Convert relative path to network absolute path
- * @param string $file
- * @return string
- */
- function dirToHttpUrl($file) {
-
- //Determine whether the file exists
- if (!file_exists($file)) {
- return false;
- }
-
- //Domain name
- $nowUrl = dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']); //Current domain name
- $tempUrl = explode('.', $_SERVER['HTTP_HOST' ]);
- $dirUrl = 'http://www.'.$tempUrl[1].'.'.$tempUrl[2].'/'; //Main domain name
-
- //Level statistics of file paths
- $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);
- }
-
- //Domain name level statistics
- $ tempWeb = explode('/', $nowUrl);
- $tempWeb = array_slice($tempWeb, 3);
- $webNum = count($tempWeb);
-
- //Domain name corresponding to the file
- if ($fileNum > $ webNum) {
- $nowUrl = $dirUrl;
- }
-
- //Return
- return $nowUrl.$fileEnd;
-
- }
-
- //dirToHttpUrl('./1.jpg');
-
Copy code
|
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
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31