Activity Stream
48,167 MEMBERS
6865 ONLINE
besthostingforums On YouTube Subscribe to our Newsletter besthostingforums On Twitter besthostingforums On Facebook besthostingforums On facebook groups

Page 1 of 3 123 LastLast
Results 1 to 10 of 21
  1.     
    #1
    Member

    Default [PHP] Get Folder Name In A Location

    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





    viruz99 Reviewed by viruz99 on . [PHP] Get Folder Name In A Location 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 ? Rating: 5

  2.   Sponsored Links

  3.     
    #2
    Member
    Website's:
    Elite.SO Defendos.com Motionite.com
    So you want to return all folder names in:
    /...../location/

    ?

    Defendos BETA3 Released! Thread - Official Website

  4.     
    #3
    Member
    Website's:
    wscripts.net damnlolscript.com lulzjet.com
    if you have the php file in folderx you can use http://php.net/getcwd

  5.     
    #4
    Member
    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";








  6.     
    #5
    Retired NinJa
    Website's:
    loledhard.com
    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;
    }



    You don't hate Justin bieber.You hate the fact you ain't Justin Bieber!

  7.     
    #6
    Respected Member
    Website's:
    DL4Everything.com Soft2050.in
    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 Code: 
    <?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

  8.     
    #7
    Member
    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"









  9.     
    #8
    Respected Member
    Website's:
    DL4Everything.com Soft2050.in
    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.

  10.     
    #9
    Member
    Edit - working tho its giving the full location

    e.g : /..../folder/foldername








  11.     
    #10
    Respected Member
    Website's:
    DL4Everything.com Soft2050.in
    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 Code: 
    $location "/....../folder/"
    If yes, then you can't add so much dots. Only ./ or ../

    You have to use something like this for it then: /../../../

Page 1 of 3 123 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Mass rar each folder
    By Ruriko in forum Technical Help Desk Support
    Replies: 1
    Last Post: 11th Aug 2012, 01:24 PM
  2. Problem with folder.
    By sasa037 in forum Technical Help Desk Support
    Replies: 4
    Last Post: 25th Dec 2011, 06:42 AM
  3. Track Folder Changes
    By Daniel in forum Webmaster Resources
    Replies: 0
    Last Post: 3rd Nov 2011, 02:06 PM
  4. VPS Folder Permissions.
    By Trueno22 in forum Server Management
    Replies: 8
    Last Post: 23rd Aug 2010, 09:59 PM
  5. [WTS] Database Folder.
    By Luke in forum Completed Transactions
    Replies: 3
    Last Post: 20th Sep 2009, 05:01 PM

Tags for this Thread

BE SOCIAL