Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to find directories?

    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:

    Code:
    <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 Code:
    <?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.....
    Last edited by RTrev; 03-21-2007 at 02:21 AM.
    Think slow, type fats

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    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).

  • #4
    New Coder
    Join Date
    Mar 2007
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    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.
    Think slow, type fats

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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().
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #6
    New Coder
    Join Date
    Mar 2007
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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!
    Think slow, type fats

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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.

  • #8
    New Coder
    Join Date
    Mar 2007
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by aedrin View Post
    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.
    Think slow, type fats

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    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).

  • #10
    New Coder
    Join Date
    Mar 2007
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by aedrin View Post
    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!!!
    Think slow, type fats

  • #11
    New Coder
    Join Date
    Mar 2007
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fou-Lu View Post
    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!
    Think slow, type fats

  • #12
    Regular Coder the-dream's Avatar
    Join Date
    Mar 2007
    Location
    Northamptonshire, UK
    Posts
    477
    Thanks
    8
    Thanked 4 Times in 4 Posts
    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

  • #13
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by the-dream View Post
    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?

  • #14
    New Coder
    Join Date
    Mar 2007
    Posts
    63
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by the-dream View Post
    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.)

    Thanks again!

    Bob
    Think slow, type fats


  •  

    Posting Permissions

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