Reputation: 30691
I have a php function that is moving files for me. It requires absolute paths to place those files (/Applications/MAMP/HTdocs/mysite/myfolder
)
how can a turn this folder path string into a url (http://mysite.com/myfolder) so that I can create links to the files on the fly?
I do not know necessarily the names of the folders, as the software could be run in many locations.
Many thanks.
Upvotes: 2
Views: 4690
Reputation: 157863
Obviously, you need to know server root for such a calculation.
Luclikly, $_SERVER['DOCUMENT_ROOT'] contains this path.
So, just subtract server root path from the given path:
$path = '/Applications/MAMP/HTdocs/mysite/myfolder';
$approot = substr($path,strlen($_SERVER['DOCUMENT_ROOT']));
check if you have a drive letter in the DOCUMENT_ROOT, and correct the code if necessary
Note that adding http://mysite.com
is unnecessary and useless. just /myfolder/
is what you really need.
Upvotes: 3
Reputation: 14173
You can check for this value: $_SERVER["DOCUMENT_ROOT"];
That is the root of your website. If you have the folder and replace the $_SERVER["DOCUMENT_ROOT"] with the $_SERVER["HTTP_HOST"] you will get the URL to the folder/file
Upvotes: 2
Reputation: 3631
If mysite folder is within HTdocs then you can access it using http://yourdomain/mysite, (if HTodcs is your home directory)
Upvotes: 0