For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > October 2006 > folder name as a variable









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author folder name as a variable
matt@londonstudent.co.uk

2006-10-30, 7:03 pm

Hi all,

I have:



function current_dir()
{
$path = dirname($_SERVER[PHP_SELF]);
$position = strrpos($path,'/') + 1;
print substr($path,$position);
}
current_dir();


Which prints to the screen the folder name of whhere the php file is
that's running this script.

Now I'm still trying to learn php but my head is hurting and so I was
hoping someone could answer this so I don't have to read up for another
couple of hours!...

But I don't want this script to print the current directory to the
screen, I just want it to be saved to a variable in the file such as
$foldername (so that I can do other stuff with this variable.

But I can't think of a way to stop it printing to the screen and also
to save as a variable.

I have tried all sorts of things like $foldername=current_dir();
But this just isn't working... am I missing something obvious??
I don't know why the statement current_dir(); prints to the screen also
(because I don't have a print or echo statement there...

Any help appreciated.

Thanks

petersprc@gmail.com

2006-10-30, 7:03 pm

Hi,

What you said, "$foldername=current_dir();" should work. You'll just
need to add "return substr($path,$position);" as the last line of the
function.

matt@londonstudent.co.uk wrote:
> Hi all,
>
> I have:
>
>
>
> function current_dir()
> {
> $path = dirname($_SERVER[PHP_SELF]);
> $position = strrpos($path,'/') + 1;
> print substr($path,$position);
> }
> current_dir();
>
>
> Which prints to the screen the folder name of whhere the php file is
> that's running this script.
>
> Now I'm still trying to learn php but my head is hurting and so I was
> hoping someone could answer this so I don't have to read up for another
> couple of hours!...
>
> But I don't want this script to print the current directory to the
> screen, I just want it to be saved to a variable in the file such as
> $foldername (so that I can do other stuff with this variable.
>
> But I can't think of a way to stop it printing to the screen and also
> to save as a variable.
>
> I have tried all sorts of things like $foldername=current_dir();
> But this just isn't working... am I missing something obvious??
> I don't know why the statement current_dir(); prints to the screen also
> (because I don't have a print or echo statement there...
>
> Any help appreciated.
>
> Thanks


matt@londonstudent.co.uk

2006-10-30, 7:03 pm

Thank you so much.

A great help, it was the "return substr" that did it like you said.

Koncept

2006-10-30, 7:03 pm

In article <1162102654.149125.255170@k70g2000cwa.googlegroups.com>,
<matt@londonstudent.co.uk> wrote:

> function current_dir()
> {
> $path = dirname($_SERVER[PHP_SELF]);
> $position = strrpos($path,'/') + 1;
> print substr($path,$position);
> }
> current_dir();


What you have constructed here is the same thing as calling
basename($path). If you want the current directory, you can call
getcwd(). If you want the name of the directory of a path, you can call
dirname($path). Here is an example:

<?php
$self = $_SERVER['PHP_SELF'];
$yourMethod = substr($self,strrpos($path,'/')+1);
$sameThing = basename($self);
$dirName = dirname($self);
$baseDir = ($bdn=basename($dirName))!=""?$bdn:$dirName;

echo "<pre>";
echo "\$_SERVER['PHP_SELF']: {$self}\n";
echo "Your method: {$yourMethod}\n";
echo "Using basename(): {$sameThing}\n";
echo "Current Directory: {$dirName}\n";
echo "Base directory: {$baseDir}\n"
echo "</pre>\n";
?>

--
Koncept <<
"The snake that cannot shed its skin perishes. So do the spirits who are
prevented from changing their opinions; they cease to be a spirit." -Nietzsche
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com