Web Hosting Talk







View Full Version : Reading the name of a folder with PHP


raulgonzalez
05-10-2005, 09:26 PM
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.

intransit
05-10-2005, 09:40 PM
Try this:

$dir=explode('/',$PHP_SELF);

Then just figure out which $dir[x] corresponds to the user's name.

raulgonzalez
05-10-2005, 09:46 PM
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.

intransit
05-10-2005, 09:53 PM
Er, you would do this:

$dir=explode('/',$PHP_SELF);
print_r($dir);

Then pick which # corresponds to the user's name. Then:

echo $dir[#];

will give you the user's name.

Burhan
05-11-2005, 09:45 AM
You want to get a list of all directories?


foreach(glob("*",GLOB_ONLYDIR) as $dir) { echo $dir."<br />"; }

TehBooster
05-11-2005, 12:50 PM
<?php echo basename(dirname( $_SERVER['PHP_SELF'] )); ?>

will output the name of the current directory, which is what you're wanting I believe.