Say I want to find a file in one of the parent directories of the script. Consider this file tree:
Folder0
Folder1
needle.php
Folder2
Folder3
script.php
I'm running script.php
and I need to find which directory contains needle.php
. I do this:
while (!file_exists($nameFile)) {
chdir('..');
}
return getcwd();
This returns "Folder0\Folder1\";
What is the proper way to transform this path into a URL of this file? (e.g. `http://hostname.com/Folder0/Folder1/needle.php"
)
Or is there a better way to get a URL of a file on a server?
getcwd()
give You current dir from server root like /home/vincent/public_html/folder You Can get DOCUMENT_ROOT By
$_SERVER['DOCUMENT_ROOT'];// /home/vincent/public_html
You Must Remove DOCUMENT_ROOT from first of getcwd and concat with Host Name. like this
echo str_replace("\\",'/',"http://".$_SERVER['HTTP_HOST'].substr(getcwd(),strlen($_SERVER['DOCUMENT_ROOT'])));
output:
http://www.domain.com/folder