[PHP] Get Folder Name In A Location

Status
Not open for further replies.

viruz99

Active Member
1,547
2010
96
0
Hey Guys ,

Any idea how to get a name of a folder via php .

e.g : /...../location/folderx

i don't have the name of the folderx ,

how can i get the folder name via php ?

* There is only 1 folder in that location





 
20 comments
nope , each time i run the script , a new folderx is created in that location with different name .
need a quick php code to retrieve its name .

@ l0calhost , yep + there is only 1 folder

$folderx = "foldername";







 
IF U WANT current DIR then getcwd() would do ur work or else if u want to search in certain dir use something like
$handler = opendir("/home/wheretosearch/");
while ($file = readdir($handler))
{
echo $file;
}
 
Ok, se we have a folder name: "unknown" in the folder "folder" from relative path

The process is somewhat similar to opendir but i prefer to use glob() for this purpose : http://php.net/manual/en/function.glob.php
Use is_dir function to check whether the specified resource is a directory: http://php.net/manual/en/function.is-dir.php

Check out the below code, i have also commented it a little:
PHP:
<?php

$folder = array(); // Creates a array for use later

foreach (glob("folder/*") as $thefolder) { // Use for each to go through and get each folder & file in the given directory
    If (is_dir($thefolder)) { // We only want to get folders so we are making sure that we are adding a directory and not a file.
        $folder[] = $thefolder; // Adds the file to the array created
    }
}

echo $folder[0]; // 0 for 1st dir, 1 for 2nd and so on...

?>

I prefer to use glob because we can specify the syntax to search for all filenames :)
 
thx soft2050 but i'm getting this error :

Parse error: syntax error, unexpected T_FOREACH on line

foreach (glob("$location\*") as $thefolder) {

* tried the direct location as well path/folder/ instead of $location

@ Humour - its working tho i'm getting "...foldername"








 
Last edited:
I was not using any IDE, was writing in notepad++ so came out with a error

I forgot to add ; at the end on line 3
Between, i have already edited before your reply but you probably might wont have saw it.
 
I have just tried the code and it does work here. The error is because the glob didn't find any file/folder in that location and couldn't add anything to array

Try using realpath() function to check whether you are on the right path: http://php.net/manual/en/function.realpath.php

Theres some problem with the location you are trying to get folder from

Edit:
Is this real what you are using in your code:
PHP:
$location = "/....../folder/";

If yes, then you can't add so much dots. Only ./ or ../

You have to use something like this for it then: /../../../
 
Yep i was using the previous code , $files instead of $folder

Its working now but its giving the full location , needed the foldername only but it might be possible to use the full location as well in the script , will have to mess with it a bit

Thanks again for your help (y)





 
Zip hit another glitch ,

Soft2050 method is working tho giving the full filelocation .

I need to rename folderx as well , remove empty spaces e.g :

"The Folder X" > "The.Folder.X"


$folder = array();
foreach (glob("$location*") as $thefolder) {
If (is_dir($thefolder)) {
$folder[] = $thefolder;
$folderx = $folder[0];
}
}


Is there any additional code i can add for this ?

when i run the above code i get : /location/The Folder X







 
between you sure it will rename the folder itself or only the variable ?

Edit : tested ( it only removes the spaces in the variable , not the folder itself )





 
OOpz! Didn't knew you need to rename the folder too:

For renaming, use rename() : http://php.net/manual/en/function.rename.php

PHP:
foreach (glob("$location*") as $thefolder) { 
    If (is_dir($thefolder)) {  
        $folderx = str_replace(' ', '.', basename($thefolder));
        rename($thefolder, str_replace(' ', '.', $thefolder);
        break; // Breaks next loops since we only need 1
    }
}

I have removed the array part since you don't really need it.
 
GncWhp.png






 
Nearly working 100% :

foreach (glob("$location*") as $thefolder) {
If (is_dir($thefolder)) {
rename($thefolder, str_replace(' ', '.', $thefolder));
$folderx = $thefolder;
break; // Breaks next loops since we only need 1
}
}

echo $folderx;

now the actual folder is renamed to The.Folder.X but $folderx is still returning = "/location/The Folder X"





 
Last edited:
PHP:
foreach (glob("$location*") as $thefolder) { 
    if (is_dir($thefolder)) {
        $folderx=str_replace(' ', '.', $thefolder)
        rename($thefolder, $folderx);
        break; // Breaks next loops since we only need 1
    }
}

echo $folderx;
 
Status
Not open for further replies.
Back
Top