...

View Full Version : How to find directories?



RTrev
03-21-2007, 03:10 AM
This seems weird to me. My 1&1 account puts me in a root directory of my own, and from anywhere in any sub-directory I can, for example, reference an image by using something like:


<img src="/images/rtrev.jpg" etc...

But when I try to get the number of images avaliable in a directory, I have to specifically back up to the root and then specify the directory:



<?php

// get a count of the files in a directory

$dir = "../images/homepage/";

$i = 0;

if ($dh = opendir($dir)) {
while ($file = readdir($dh)) {
$i++;
}
closedir($dh);

echo "$i";

}
?>


Is there something in the .htaccess file that's missing? I'm unclear on the DocumentRoot, for example. But everything seems to work fine except when I'm using PHP such as this opendir function.

Anyone have any thoughts what I misunderstand? Thanks.....

Fou-Lu
03-21-2007, 04:06 AM
HTML paths work a little differently.
/images is searching from the directory root if I'm not mistaken. So, if your on apache you are probably working in something like:
root
-- images
-- public (or httpdocs or something that indicates home)
where public is the location of your actual html pages and images are the storing of your images.

PHP you are referencing back from the page being accessed:
../images
says to look at the directory above me (public => root) and grab images (root => images). So this structure would work.
Otherwise, I'm not certain what your question is, your php code itself will work, but watch the check on it, it will also count your current directory (.) and parent directory (..), as well as any interior directories. Check using an is_dir function before incrementing your i.
Is this what you are looking for?

Inigoesdr
03-21-2007, 04:18 AM
In HTML if you prefix the path with a / the browser will start in the root directory of the public folder(ie. yourdomain.com/) and go from there. PHP uses filesystem paths, so you would use "images/" if you're in the root directory(or ../images/ if you're in another directory on the same level as the images folder).

RTrev
03-21-2007, 04:30 AM
In HTML if you prefix the path with a / the browser will start in the root directory of the public folder(ie. yourdomain.com/) and go from there. PHP uses filesystem paths, so you would use "images/" if you're in the root directory(or ../images/ if you're in another directory on the same level as the images folder).

Thanks. I wonder if there's a way to make more portable code by somehow defining a root directory which everything in a given project is then relative to? I'll have to do some more studying. :)

Fou-Lu
03-21-2007, 05:50 AM
Yes you can change the base directories.
HTML uses a <base> tag to define its base directories. Word of warning, relative <a> also make use of the base tag.
In php, change the current working directory using cwd().

RTrev
03-21-2007, 02:19 PM
Yes you can change the base directories.
HTML uses a <base> tag to define its base directories. Word of warning, relative <a> also make use of the base tag.
In php, change the current working directory using cwd().

Okay, I think I'm with you. Presumably the cwd() would follow the same rules as PHP in that I wouldn't cwd to /images but rather to images. I'll have to play with it when I get home.

I was thinking there was a way in .htaccess to say "this is the root / directory to use for everything running in this area. And that all would obey that. I was astounded to try for a directory of just "/" using PHP and seeing things I'd never seen before with FTP. It really did take me to the root directory, or at least to a level higher than I'd ever been before, and am not even sure I'm supposed to be able to get there.

I'll figure it out with the hints you've all given me. Thanks!

aedrin
03-21-2007, 03:56 PM
Presumably the cwd() would follow the same rules as PHP in that I wouldn't cwd to /images but rather to images

Don't change to the right directory everytime like you would in a console prompt.

Change the current working dir to whatever you want the root to be, then base all of your paths on that root. This way you need to only change 1 path when moving things around.

RTrev
03-21-2007, 04:22 PM
Don't change to the right directory everytime like you would in a console prompt.

Change the current working dir to whatever you want the root to be, then base all of your paths on that root. This way you need to only change 1 path when moving things around.

Okay, sorry to be dense.. but it seems like this would complicate things even more. For example, I have some family pages, and sub-directories under those for each family, and under those some more sub-directories for things like wedding pictures, etc. And from *those* pages I have been grabbing images in HTML by simply pointing to /images/whatever_image.jpg. But these pages also need to reference the directory immediately above them, and also back to the home page. Would it make sense to actually get the lengthy pathname of my own personal root directory area and specify that for each project? In other words I'm just sitting in the "root" and reaching out in whatever direction I need from there?

I wish I could duplicate the behavior of the HTML, where /someplace always seems to work.

Hard to wrap my mind around this while I'm pretending to work. :D

aedrin
03-21-2007, 05:00 PM
In other words I'm just sitting in the "root" and reaching out in whatever direction I need from there?

In my opinion this is the easiest. You only need to change 1 path when you move the application to something that requires a different root.

This is on the PHP side of course.

In HTML, it might be useful to use the <base> element as Fou-Lu suggested, then use that as the "HTML root".

Some of these things might be different in case you have a unique setup. ;)

If you access all generic images using /images/generic.gif, and then album specific images using ../images/picture1.jpg, then you should have no problem. In that case you would set the <base> element to whatever you consider the site's root to be (from where generic images are located).

RTrev
03-21-2007, 05:11 PM
In my opinion this is the easiest. You only need to change 1 path when you move the application to something that requires a different root.

This is on the PHP side of course.

In HTML, it might be useful to use the <base> element as Fou-Lu suggested, then use that as the "HTML root".

Some of these things might be different in case you have a unique setup. ;)


Yes, that sounds good! And I thought I had a fair grasp of HTML, but I've never encountered that "base" tag before. (Thanks, Fou-Lu!)

You've been a great help to me.. a sincere thank you!!!

RTrev
03-21-2007, 05:15 PM
HTML paths work a little differently.
/images is searching from the directory root if I'm not mistaken. So, if your on apache you are probably working in something like:
root
-- images
-- public (or httpdocs or something that indicates home)
where public is the location of your actual html pages and images are the storing of your images.

PHP you are referencing back from the page being accessed:
../images
says to look at the directory above me (public => root) and grab images (root => images). So this structure would work.
Otherwise, I'm not certain what your question is, your php code itself will work, but watch the check on it, it will also count your current directory (.) and parent directory (..), as well as any interior directories. Check using an is_dir function before incrementing your i.
Is this what you are looking for?

Yes, thanks, you've nailed my question precisely. I thought I had a iron-clad root that I could use from either HTML or PHP.. and found the hard way that it isn't quite like that. ;) This helps a lot!

the-dream
03-22-2007, 12:32 AM
Hi RTrev,

You can use my code but do not use it commercially please. I did write that script and use it on here to get formatting help!

If you want to use it commercially please give my website some cred:
www.the-dream.co.uk

thanks

Inigoesdr
03-22-2007, 07:01 AM
Hi RTrev,

You can use my code but do not use it commercially please. I did write that script and use it on here to get formatting help!

If you want to use it commercially please give my website some cred:
www.the-dream.co.uk

thanks
Did I miss something?

RTrev
03-22-2007, 12:22 PM
Hi RTrev,

You can use my code but do not use it commercially please. I did write that script and use it on here to get formatting help!

If you want to use it commercially please give my website some cred:
www.the-dream.co.uk

thanks

Hi, and thanks for the kind offer! I am not sure exactly what code you're referring to here.. but I kind of like rolling my own since I'm in the learning phase and there's no better way to learn than by screwing it all up. ;) I may be back with my tail between my legs asking to look at your code at some point, but for now it seems like it will be straightforward and the tough part will just be finding a directory structure to base all of my apps on and which will be easily transportable to other platforms if 1&1 can't solve my session problems (but that's another thread.) :D

Thanks again!

Bob



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum