Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

creating a path appending folders... how to add them???

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

creating a path appending folders... how to add them???

Postby imroue » Sat Oct 12, 2002 8:54 am

I am trying to create a path by passing the folder name as a var.
for example:
Code: Select all
<?php
$myfolder = "graphics";
if ($handle = opendir($HTTP_SERVER_VARS['DOCUMENT_ROOT'])) {
    echo "Files:\n";
?><br><?php
    while (false !== ($file = readdir($handle))) {
   $mydir = dirname($file);
   $mydir = $mydir+"/" +$graphics; <----HERE IS the problem?
   $file = basename ($file,".htm");
                echo "$file\n";
   echo "$mydir\n";
?>


mydir is now the root directory, but i want to give it a folder name (that exists of course) and make it ouput that full path
User avatar
imroue
New php-forum User
New php-forum User
 
Posts: 62
Joined: Fri Oct 11, 2002 6:53 am

Postby Xerpher » Sat Oct 12, 2002 9:19 am

I'm quite sure PHP doesn't use +'s. Try replacing the +'s with .'s (periods)

[code]$mydir = $mydir."/".$graphics; <----HERE is the solution /code]
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby imroue » Sat Oct 12, 2002 9:31 am

Xerpher wrote:I'm quite sure PHP doesn't use +'s. Try replacing the +'s with .'s (periods)

[code]$mydir = $mydir."/".$graphics; <----HERE is the solution /code]


THANKS!!! That did it. (back to the drawing board!) :D :D
User avatar
imroue
New php-forum User
New php-forum User
 
Posts: 62
Joined: Fri Oct 11, 2002 6:53 am

Re: creating a path appending folders... how to add them???

Postby Jay » Sat Oct 12, 2002 2:07 pm

imroue wrote:I am trying to create a path by passing the folder name as a var.
for example:
Code: Select all
<?php
$myfolder = "graphics";
if ($handle = opendir($HTTP_SERVER_VARS['DOCUMENT_ROOT'])) {
    echo "Files:\n";
?><br><?php
    while (false !== ($file = readdir($handle))) {
   $mydir = dirname($file);
   $mydir = $mydir+"/" +$graphics; <----HERE IS the problem?
   $file = basename ($file,".htm");
                echo "$file\n";
   echo "$mydir\n";
?>


mydir is now the root directory, but i want to give it a folder name (that exists of course) and make it ouput that full path

I would amend your code slightly, see if you can spot the difference:
Code: Select all
<?php
$myfolder = "graphics";
if ($handle = opendir($HTTP_SERVER_VARS['DOCUMENT_ROOT'])) {
    echo "Files:<br>\n";
    while ($file = readdir($handle)) {
   $mydir = dirname($file);
   $mydir = $mydir."/".$graphics;
   $file = basename($file.".htm");
                echo "$file\n";
   echo "$mydir\n";
    }
}
?>
Jay
 

Re: creating a path appending folders... how to add them???

Postby imroue » Sat Oct 12, 2002 2:45 pm

mydir is now the root directory, but i want to give it a folder name (that exists of course) and make it ouput that full path[/quote]
I would amend your code slightly, see if you can spot the difference:
Code: Select all
<?php
$myfolder = "graphics";
if ($handle = opendir($HTTP_SERVER_VARS['DOCUMENT_ROOT'])) {
    echo "Files:<br>\n";
    while ($file = readdir($handle)) {
   $mydir = dirname($file);
   $mydir = $mydir."/".$graphics;
   $file = basename($file.".htm");
                echo "$file\n";
   echo "$mydir\n";
    }
}
?>


Thanks, that works too, but I just realized, I 've been going around in circles...the var $file contains files in the ROOOT directory...I was trying to get the root directory so I can append the graphics directory to it..and THEN read off the file names in $graphics.... NOW that would solve the real issue??? Eventually, I would want to only change the var $graphics and be able to use this page anywhere ....

I tried this.. but of course as a newbie..it didn't work:
opendir($HTTP_SERVER_VARS['DOCUMENT_ROOT']."/".$graphics))

Any ideas?
User avatar
imroue
New php-forum User
New php-forum User
 
Posts: 62
Joined: Fri Oct 11, 2002 6:53 am

Postby Xerpher » Sat Oct 12, 2002 4:39 pm

Well in my experience, putting array values in a function never seems to work. It could just be me, I truly dont know, but I know that on my server I would have to put this...

Code: Select all
$rootdir = $HTTP_SERVER_VARS['DOCUMENT_ROOT'];

...BEFORE your opendir and change it to this...

Code: Select all
opendir($rootdir."/".$graphics))

I hope Jay reads this and tells me if its some kind of glitch on my server that I have to do this or if its normal.
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby Jay » Sun Oct 13, 2002 1:10 am

Must be a glitch, it's definately not normal! If a function requires a parameter and that parameter is an array key, you can reference the key directly to serve as the parameter

opendir($HTTP_SERVER_VARS['DOCUMENT_ROOT']."/".$graphics) should work!
Jay
 

Re: creating a path appending folders... how to add them???

Postby Jay » Sun Oct 13, 2002 1:14 am

imroue wrote:mydir is now the root directory, but i want to give it a folder name (that exists of course) and make it ouput that full path

I would amend your code slightly, see if you can spot the difference:
Code: Select all
<?php
$myfolder = "graphics";
if ($handle = opendir($HTTP_SERVER_VARS['DOCUMENT_ROOT'])) {
    echo "Files:<br>\n";
    while ($file = readdir($handle)) {
   $mydir = dirname($file);
   $mydir = $mydir."/".$graphics;
   $file = basename($file.".htm");
                echo "$file\n";
   echo "$mydir\n";
    }
}
?>


Thanks, that works too, but I just realized, I 've been going around in circles...the var $file contains files in the ROOOT directory...I was trying to get the root directory so I can append the graphics directory to it..and THEN read off the file names in $graphics.... NOW that would solve the real issue??? Eventually, I would want to only change the var $graphics and be able to use this page anywhere ....

I tried this.. but of course as a newbie..it didn't work:
opendir($HTTP_SERVER_VARS['DOCUMENT_ROOT']."/".$graphics))

Any ideas?[/quote]
IF you want to read the the graphics directory just do this
Code: Select all
<?php
$myfolder = "graphics";
if ($handle = opendir($HTTP_SERVER_VARS['DOCUMENT_ROOT'].$myfolder)) {
    echo "Files:<br>\n";
    while ($file = readdir($handle)) {
                print "$file<br>\n";
    }
}
?>
Jay
 


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.