Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2003
    Location
    laredo
    Posts
    303

    Reading the name of a folder with PHP

    Hello. I sell books online, through amazon.com and as my inventory got larger I decided to put up a webpage for those books so I bought my self a very usefull php book.
    At that time I learned to write to MySql database and blabla.

    After a few days I decided that I could make it more useful if I allowed others to sell their own books on my site so I did a little something and now when some one registeres to my site, they get their own folder which in turns is the same name as their username;
    and copies of the original project.

    Now here is the thing. For example if Joe registers he gets.

    http://mysite.com/joe/index.php

    up until now it has been working ok, but the way I am pulling it out is the one way I kind of don't think is the right way.

    This is what I do.

    At the time of registration one of the files gets created with the name of the registree inside the file. Here is an example.

    //Escribiendo A El Archivo name.txt
    $newfile = fopen("../$name/include_info/name.txt", "w");
    fwrite($newfile, "<? \$name = \"$name\";?>");
    fclose($newfile);

    ..............................................................
    $name is the $name of the Username.

    so I end up with a file like:
    <? $name = "joe";?>

    then in order to read joe's page I have that file included. example:

    <?include ("name.txt")?>
    and bla bla bla
    more code
    bla bla
    and finlally
    $query = "SELECT * FROM information where name='{$name}'";

    so that gives me joe's information on
    http://mysite/joe/index.php
    ..................................


    I did this out of my butt and even though it works I was wondering if there is a better way to read the name of a folder instead of writing a file with the username's name on it.

    example: a code inside any page within the /joe/ folder will return:
    "joe".

    or if anyone has a different idea. I was doing this for fun but after a while I thought it was interesting and that is why I now want to fix the code clean.

    Thanks in advance.
    Last edited by raulgonzalez; 05-10-2005 at 09:33 PM.

  2. #2
    Join Date
    Jan 2005
    Posts
    45
    Try this:
    PHP Code:
    $dir=explode('/',$PHP_SELF); 
    Then just figure out which $dir[x] corresponds to the user's name.

  3. #3
    Join Date
    Jul 2003
    Location
    laredo
    Posts
    303

    * Hi

    I tried this and it gave me the word "Array"

    What did I do wrong?

    <?
    $dir=explode('/',$PHP_SELF);
    echo "$dir";
    ?>

    I know that if I echo this

    echo "$PHP_SELF";

    it will give me the name of the directory, but along with the rest of the dirs and the file name containing the script.

  4. #4
    Join Date
    Jan 2005
    Posts
    45
    Er, you would do this:
    PHP Code:
    $dir=explode('/',$PHP_SELF);
    print_r($dir); 
    Then pick which # corresponds to the user's name. Then:
    PHP Code:
    echo $dir[#]; 
    will give you the user's name.

  5. #5
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    You want to get a list of all directories?

    PHP Code:
    foreach(glob("*",GLOB_ONLYDIR) as $dir) { echo $dir."<br />"; } 
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  6. #6
    Join Date
    Dec 2003
    Location
    Clarksville, Indiana
    Posts
    75
    PHP Code:
    <?php echo basename(dirname$_SERVER['PHP_SELF'] )); ?>
    will output the name of the current directory, which is what you're wanting I believe.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •