getcwd () string question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Riverside
Forum Newbie
Posts: 7
Joined: Thu Apr 14, 2011 12:49 pm

getcwd () string question

Post 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?
adityamenon90
Forum Newbie
Posts: 7
Joined: Fri Apr 16, 2010 1:41 pm

Re: getcwd () string question

Post 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.
Riverside
Forum Newbie
Posts: 7
Joined: Thu Apr 14, 2011 12:49 pm

Re: getcwd () string question

Post 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.
Riverside
Forum Newbie
Posts: 7
Joined: Thu Apr 14, 2011 12:49 pm

Re: getcwd () string question

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: getcwd () string question

Post 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);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Riverside
Forum Newbie
Posts: 7
Joined: Thu Apr 14, 2011 12:49 pm

Re: getcwd () string question

Post 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)
Post Reply