Jump to content

Recommended Posts

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? 

Link to comment
https://forums.phpfreaks.com/topic/310002-folder-path-without-the-varwwwhtml-php/
Share on other sites

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();

 

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.

  • Like 1

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 by Strider64
two the's
  • Great Answer 1

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... 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.