Displaying Images form a Directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jim Carlock

    #16
    Re: Displaying Images form a Directory

    "Jameson" <jameson_ray@co mcast.net> wrote:[color=blue]
    > If we take a look at it...[/color]

    <?php
    $dir = '.';
    $sAltText = "Picture";
    foreach (glob("*.jpg") as $file) {
    echo "file: $file<br />\n";
    echo "<i>filenam e:</i> <b>$file</b>, <i>filetype:</i> <b>" .
    filetype($file) . "</b><br />\n";
    echo '<img src="' . $file . '" border="0" alt="$sAltText" /><br />' .
    "\n";
    }
    ?>
    [color=blue]
    > ...Let's say we set the directory to $dir = '/Users/jray/Pictures';
    > (a path that I can browse to just fine from the command line).
    > Where is the $dir variable used in the rest of the script?[/color]

    I used the $dir in one of the scripts and forgot to take it out this
    one, or perhaps left it on purpose (unknown reason). :-)

    The single dot indicates the current directory, two dots indicate
    the parent directory of the current directory.

    $dir = "..";

    You could also use it as the path to the folder you want to parse
    and applie the file exstension inside of it...

    $dir = "../*.jpg";
    [color=blue]
    > If a directory is specified in the glob() function, I believe it
    > just defaults to the directory that the script is in.[/color]

    That's exactly the way I see it. No difference. I find it much
    better to use relative paths right at the moment, using

    "../images/*.jpg"

    to get to the folder in question, rather than absolute paths,

    "/images/*.jpg"

    The script would read...

    <?php
    $dir = '/images/*.jpg';
    $sAltText = "Picture";
    foreach (glob($dir) as $file) {
    echo "file: $file<br />\n";
    echo "<i>filenam e:</i> <b>$file</b>, <i>filetype:</i> <b>" .
    filetype($file) . "</b><br />\n";
    echo '<img src="' . $file . '" border="0" alt="$sAltText" /><br />' .
    "\n";
    }
    ?>

    As long as the web-root is set up properly, the "absolute
    path" works fine on a website... let your root path be
    '/Users/jray/' and the php file resides in, '/php', the images
    reside in, '/images/'. So a php file in the php folder could
    reference image files by using '../images/img.jpg' or
    '/images/img.jpg'.

    If you employed Explorer on a Windows system, you could
    change from the currently viewed folder to another folder
    by typing in ..\WINDOWS, or "..\Program Files\", or
    ...\WINDOWS\sys tem32\ into the address bar. Someone
    had a patent or some such on using the slash to denote paths.
    Thus Microsoft patented the backslash concept (don't quote
    me though as I can't identify the source read years ago and
    maybe my mind plays tricks on me).

    It works the same way on Unix systems but you change the
    backslashes to forward slashes.

    I imagine it works similar on a Mac, but I'll let someone
    with Mac experience confirm it. Let me know.
    [color=blue]
    > Thanks again for sticking with this. Sometimes is is good
    > to have an understanding of how a function works across
    > different operating systems.[/color]

    Yes, especially when it all jives together and works in the
    same or similar manners.

    Hope that helps.

    Jim Carlock
    Post replies to the newsgroup.


    Comment

    Working...