Reputation: 13263
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?
Upvotes: 1
Views: 11896
Reputation: 3149
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
Upvotes: 6
Reputation: 2019
$basePath="http://hostname.com/"; $fileName="needle.php"; $folderPath="Folder0\Folder1\";
$path=str_replace("\","/",$folderPath); $pathToFile=$basePath.$path.$fileName;
echo $pathToFile;
Make a function out of it !
Lucian
Upvotes: 0
Reputation: 57
while (!file_exists($nameFile)) {
chdir('..');
}
return "http://".$_SERVER['HTTP_HOST'].str_replace('\\', '/', getcwd());
Upvotes: 0
Reputation: 2763
You can use The RecursiveDirectoryIterator class
and here is a good tutorial that might help you
I hope this help, if anymore help needed we are here and glad to help :)
Upvotes: 0