yakking Posted September 5, 2015 Share Posted September 5, 2015 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! Quote Link to comment https://forums.phpfreaks.com/topic/298066-echolist-previous-folder/ Share on other sites More sharing options...
Barand Posted September 5, 2015 Share Posted September 5, 2015 What file? There is no mention of files in your code. By "previous" do you mean the folder that the file was in before being moved to its current location? Quote Link to comment https://forums.phpfreaks.com/topic/298066-echolist-previous-folder/#findComment-1520291 Share on other sites More sharing options...
yakking Posted September 5, 2015 Author Share Posted September 5, 2015 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! Quote Link to comment https://forums.phpfreaks.com/topic/298066-echolist-previous-folder/#findComment-1520295 Share on other sites More sharing options...
Ch0cu3r Posted September 5, 2015 Share Posted September 5, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/298066-echolist-previous-folder/#findComment-1520296 Share on other sites More sharing options...
yakking Posted September 5, 2015 Author Share Posted September 5, 2015 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_folderSo 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? Quote Link to comment https://forums.phpfreaks.com/topic/298066-echolist-previous-folder/#findComment-1520300 Share on other sites More sharing options...
Ch0cu3r Posted September 5, 2015 Share Posted September 5, 2015 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); Quote Link to comment https://forums.phpfreaks.com/topic/298066-echolist-previous-folder/#findComment-1520303 Share on other sites More sharing options...
Barand Posted September 5, 2015 Share Posted September 5, 2015 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 1 Quote Link to comment https://forums.phpfreaks.com/topic/298066-echolist-previous-folder/#findComment-1520304 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.