JonnyDriller Posted February 10, 2020 Share Posted February 10, 2020 I need to assign my current folder path to a variable. I keep getting this:- Quote /var/www/html/LifeSaverHTML/Details/20 What I want is this:- Quote /LifeSaverHTML/Details/20 That full path does weird stuff. Sometimes when I'm editing files, it's like I'm editing a ghost copy but not the actual file... Weird anyways. Below is some of the attempts I've tried... If I use this command:- //$uri = $_SERVER['REQUEST_URI']; It will give me what I need but with index.php tacked onto the end:- Quote /LifeSaverHTML/Details/20/index.php <?php //$pa = $_SERVER['PATH_TRANSLATED']; //echo $pa; //$uri = string dirname ( $path ); //echo $uri; //$ echo $PWD // current directory // current directory //echo getcwd() . "\n"; //chdir('cvs'); // current directory //echo getcwd() . "\n"; //$pa = basename(__DIR__); //echo $pa; //$pa = dirname(__FILE__); //echo $pa; $cur_dir = explode('\\', getcwd()); echo $cur_dir[count($cur_dir)-1]; ?> This first one returns a blank page without error, there's one that will return just the directory most are the www stuff. I suppose I could edit the string but surely there must be a function? Quote Link to comment Share on other sites More sharing options...
JonnyDriller Posted February 10, 2020 Author Share Posted February 10, 2020 Sorry guys I got it: - $uri_dir = dirname($_SERVER['REQUEST_URI']); echo $uri_dir; Might come in handy for someone in future Quote Link to comment Share on other sites More sharing options...
JonnyDriller Posted February 10, 2020 Author Share Posted February 10, 2020 Interestingly I actually needed the whole path for that one. It wanted the complete path to send an attachment in PHPMailer. It wouldn't take the /LifeSaverHTML/Details/20 It wanted the whole /var/www/html/LifeSaverHTML/Details/20 This code does it though there's probably one of those server commands would work too. $uri_dir = getcwd(); Quote Link to comment Share on other sites More sharing options...
requinix Posted February 10, 2020 Share Posted February 10, 2020 It's generally not good design if you have to pull a path like that dynamically. A script sending the email should simply write the required /LifeSaverHTML/Details/20 or whatever directly. By the way, dirname on the REQUEST_URI can be very easily fooled. Definitely don't do that. 1 Quote Link to comment Share on other sites More sharing options...
Strider64 Posted February 10, 2020 Share Posted February 10, 2020 (edited) I personally do the following and call it a day: define("APP_ROOT", dirname(dirname(__FILE__))); define("PRIVATE_PATH", APP_ROOT . "/private"); define("PUBLIC_PATH", APP_ROOT . "/public"); require_once PRIVATE_PATH . "/vendor/autoload.php"; require_once PRIVATE_PATH . "/security/security.php"; require_once PRIVATE_PATH . "/config/config.php"; Edited February 10, 2020 by Strider64 two the's 1 Quote Link to comment Share on other sites More sharing options...
JonnyDriller Posted February 10, 2020 Author Share Posted February 10, 2020 Thanks guys for the great advise. When all this is over and done, I'm gonna revisit web development on a nice platform, with a proper text editor... i.e not a Raspberry pi that existed 2 decades ago This IOT project is gonna sit on a localhost...yeh I know... So it doesn't have to be super secure. I'm gonna do a screen recording of it and then I'm going to trim it down to make it have a much functionality as possible for a demo on the university Lan. The thing doesn't have wifi and things are not allowed on the Uni network...😂 the joys... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.