Page 1 of 1

getcwd () string question

Posted: Sun Jun 05, 2011 8:11 pm
by Riverside
I'd like to know how, if possible, to remove part of the string returned by getcwd(). I'm running a script in /home/account/public_html/sitename.com/images/users/filename.php that needs to display the current directory, but obviously I don't want the entire path shown.

It is only necessary for /users and below to be seen so my users have some idea where they are because they will be creating a lot of subfolders, but will never need access above /users.

Can someone give me an example of how to truncate the left side of the string returned by getcwd() so that only the folders I want are returned, or is there a better way to do this?

Re: getcwd () string question

Posted: Mon Jun 06, 2011 7:10 am
by adityamenon90
I love the easy questions. They make me look wise.

Store the string returned by getcwd() in a variable, then use preg_replace() or str_replace() to remove the unwanted parts.

Re: getcwd () string question

Posted: Mon Jun 06, 2011 9:06 am
by Riverside
Thank you adityamenon90. Give me a while. I'll have you feeling like a PHP god, at least for a while. :wink:

That was the approach I had planned to use, however although I do have quite a bit of programming experience, I have very little in the world of PHP so far, so I'm not very familiar with syntax and/or available object libraries.

Could you provide an example using the example string above? That would be extremely helpful.

This is how the output reads now when the script first runs:

"Current directory: /home/account/public_html/mysite.com/private/images/users/"

What I want is:

"Current directory: /users/"

Currently, getcwd() is stored as "$fulldir" ($fulldir = getcwd();"

From there, how exactly would you use preg_replace() or string_preplace() to strip "/home/account/public_html/mysite.com/private/images" from the string?

Thanks in advance.

Re: getcwd () string question

Posted: Mon Jun 06, 2011 9:26 am
by Riverside
BTW: the original script used $basedir = getcwd() for the string, but also used it to display the current directory in a folder structure on the web page, which was bad, of course, because it allowed users to navigate above public_html.

So I changed the line:

$basedir = getcwd();

to:

$basedir = dirname(__FILENAME__);

then added:

$fulldir = getcwd();

and changed:

<h3>Index of <?php echo $basedir; ?></h3>

to:

<h3>Index of <?php echo $fulldir; ?></h3>

Those changes stopped users from being able to get above the current directory "/users/" but now shows the string in its entirety (again ~ as it did originally).

Hope that makes sense.

Re: getcwd () string question

Posted: Mon Jun 06, 2011 10:15 am
by pickle
The quickest would be to use substr():

Code: Select all

$trim = "/home/account/public_html/mysite.com/private/images";
$trim_length = strlen($trim);

$trimmed = substr($trim,$trim_length);

Re: getcwd () string question

Posted: Mon Jun 06, 2011 2:20 pm
by Riverside
pickle wrote:The quickest would be to use substr():

Code: Select all

$trim = "/home/account/public_html/mysite.com/private/images";
$trim_length = strlen($trim);

$trimmed = substr($trim,$trim_length);
Okay, but I'm not seeing exactly how to relate this to $fulldir or getcwd() in the example I gave above. (3rd post)