Jump to content

Recommended Posts

I found a php code that will echo/list the current folder that the file is in:

 

 

<?php

foreach(glob('directoryname/*', GLOB_ONLYDIR) as $dir) {
    $dir = str_replace('directory/', '', $dir);
    echo $dir;
}
; ?>
 

 

Is it possible to have the same thing except echo/list the previous folder that the file is in?

 

Thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/298066-echolist-previous-folder/
Share on other sites

Hi Barand

 

Sorry I should have expanded on what I was asking.

That above code is in a php file, if you request that php file in a browser it prints/echos the current folder that the php file is in. e.g. /directoy/name/

 

So what I am asking is there a way to print/echo the folder/directory that the /directory/name/ folder is in I can't think of the terminology, I think its called parent directory. e.g. /folder<--/name/ so the php file is in the /name/ folder I would like to print the /folder/ part. Now heres the catch, the /folder/ can actually be named anything, hence why I need the code to echo/print it, it's not static.

 

Thanks for any help you can provide!

Use dirname to get the parent directory

$currentDir = getcwd();
$parentDir = dirname($currentDir);

echo "Currently in diretory: $currentDir<br />
Parent Directory is: $parentDir";

If you only want the parent directory name and not the path then apply basename to $parentDir

Thanks Ch0cu3r!

That's so close to what I am after! With those examples it returns all the folder paths e.g /home/public_html/folder_here/another_folder

So if I use basename can I get just the /folder_here/  and not the whole /home/public_html/folder_here/another_folder path?
I had a look at the basename examples in the link, is there an example that I would understand?

The PHP manual is as clear as it will get. You pass the file path as the first argument. It returns the parent directory name.

 

This is what I meant by apply basename to $parentDir

echo "Currently in diretory: $currentDir<br />
Parent Directory is: " . basename($parentDir);

There may be confusion here between basename() and dirname()
 

$dir = " /home/public_html/folder_here/another_folder"; 
   
echo basename($dir).'<br>';  //--> another_folder

echo dirname($dir).'<br>';   //--> /home/public_html/folder_here
  • Like 1
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.