Effective way to find particular directory name?
I have a script that I've written that I sell to others who want a virtual gift application on Facebook. As many of the customers who buy this script don't actually have programming knowledge, I try to simplify things as much as is possible for them by gathering as much data as I can without their input (such as using $_SERVER
vars).
I'm running into a problem when trying to establish a static directory for images, though, that the code can read and always know where that directory is.
For instance, on my 'masterfile.php' which is included on most every page I have this:
$app_info['server_url']="http://".$_SERVER['SERVER_NAME'];
$app_info['callback_url']=$app_info['server_url'].dirname($_SERVER['PHP_SELF'])."/";
$app_info['image_url']=$app_info['callback_url'].'images/';
That gives me all the necessary information I need about where folders are located... unless I'm using a directory inside the parent directory to the entire script (IE: /folder/ajax/).
I'm running into a problem where the 'images' folder needs to always be '/folder/images/' instead of ending up as '/folder/ajax/images/' when working with other directories inside the parent.
I ca开发者_C百科n't just hard code the parent directory name as some customers upload them to directories such as '/folder/folder/folder/folder/script/' - that's why I use $_SERVER['PHP_SELF']
to establish the parent directory.
Is there a way (or a variable) that I can use for this problem? I just need to ensure that the $app_info['callback_url']
is always the same, regardless of the current directory.
Thanks!
I would define a base dir constant in a bootstrap type file of your application.
define('BASE_DIR', basedir(__FILE__));
(if you can guarantee >= PHP 5.3, you can just use __DIR__
.)
Then simply include files based on this constant...
include BASE_DIR . '/something.php';
Thats how many folders the user has so many ../ will be included in the path for images.
$dir = scandir('/testdir');
$dots = '/';
for($i = 0;$i < sizeof($dir)-2;$i++) // -2 not to count . and .. as folders
{
$dots .= '../';
}
$app_info['server_url'] = "http://" . $_SERVER['SERVER_NAME'];
$app_info['callback_url'] = $app_info['server_url'] . dirname($_SERVER['PHP_SELF']) . "/";
$app_info['image_url'] = $app_info['callback_url'] . "{$dots}images/";
精彩评论