For example,

I have an empty directory named "subdirectory" path of which is as following:

Path:

/home/user/public_html/directory/subdirectory/

How shall I get or define "subdirectory" as a variable as I shall be renaming it with a database value in the following example.

$existing = "subdirectory"; // This is what I want capture from the above path and define as a variable
$new = "database_value";
$renamed = rename($existing, $new);

    Does this do it for ya?

    <?php
    
    $p = "/path/to/some/subdirectory/";
    $i = pathinfo($p);
    $new = "database_value";
    $renamed = rename($i['basename'],$new);

    🙂

      That's not it actually. May be I was unable to pin point the problem in my question.

      I quoted the whole directory path

      /home/user/public_html/directory/subdirectory/

      for convenience of readers.

      In fact, my question was how to get or define an empty directory as a variable when I only know the possible existence of a down most directory but I don't know it's name.

      For example, in the path

      /home/user/public_html/directory/subdirectory/

      I'm only aware about

      /home/user/public_html/directory/-->a  sub-directory is here but I don't know the name<--/

      but actually there is a sub-directory there named "subdirectory" in the above path which is ever changing.

      How shall I define the down most directory "subdirectory" as a variable either using basename or dirname or any other suitable function in this case?

      Thanks,

        Well, getting the last component of a path is what [man]basename[/man] does. Then again, it's also what dalecosp's use of [man]pathinfo[/man] does.

        If you want to define a variable to have that value it's [font=monospace]$subdir=basename('/..../');[/font]; I know that's the opposite of what you're saying but it is closer to what you're describing.

        It's not clear what "empty directory" is referring to - usually it means a directory that doesn't contain any files. Do you mean you want to create a new (empty) directory somewhere?

          dalecosp defined the variable $p as

          $p = "/path/to/some/subdirectory/"; where "subdirectory" is a known directory name. In this case basename("/path/to/some/subdirectory/"); shall return "subdirectory" but I want something that returns $subdirectory as a base name as it shall be renamed dynamically. "subdirectory" itself in the above code should be a variable before doing anything.

          My question is how can I extract "subdirectory" as a variable from directory path when the directory name is unknown? The predefined name of "subdirectory" can be anything on the contrary of "subdirectory" that I don't know.

          The unknown folder "subdirectory" shall be empty primarily but later on there may be some files there but we have to extract this directory variable assuming that there is no file there in it. This is the directory that shall be renamed repeatedly after extracting as a variable.

          Thanks,

            $p = "/path/to/some/subdirectory/"; where "subdirectory" is a known directory name.

            Since dalecosp didn't have your application and wrote a complete example to illustrate the use of [man]pathinfo[/man] - it certainly wasn't intended for copy-and-paste. You'd replace the definition of [font=monospace]$p[/font] (or, more accurately, you'd replace what you pass to [man]pathinfo[/man]) with whatever currently contains the path you're trying to get the basename of.

            Because, obviously, if you know that the path literally is [font=monospace]/path/to/some/subdirectory/[/font] then you don't need to do any programming to know that the last component literally is [font=monospace]subdirectory[/font].

            If you don't have the path at all, then until you do no computing in the world is going to allow you to get its last component.

              I'm not copier. Ultimately, I don't get any clue about how should a define the basename as a variable in this case.

              You must know that rename function always looks for the directory name to be renamed before performs it task and a directory that has a constant variable value can't be renamed anymore when it is done once.

              Thanks,

                I also assume that PHP hasn't developed any code to establish session or query string based interlinks between database & directory. May it's very hard to think that way.

                I guess it's better to save user level files in a unique directory which shall remain unchanged.

                Thanks,

                  so you want to find the name of a folder inside another folder, given you only know about another folder:

                  $dir = '/path/to/known/dir/';
                  $files = glob($dir . '*');
                  foreach($files as $file) {
                     if( is_dir($file) ) {
                        $unknownDirName = basename($file);
                        break;
                     }
                  }
                  
                    Write a Reply...