Displaying Images form a Directory

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

    Displaying Images form a Directory

    Happy New Year, Everyone!

    I am trying to figure out how to display a bunch of images (mainly
    JPEGs, but possibly a few GIFs and PNGs as well) that are stored in a
    local directory on the system. I can do this with the glob() function,
    but I can't seem to put in a directory other than one within the
    webroot. For example, I can only put "/uploads" and not
    "/Volumes/jray/Pictures...".

    Any ideas how to get around this? If I can't use the glob function,
    it's fine, but I only want images to be displayed (and not any other
    file that happpens to be stored in that directory).

    Thanks in advance!

  • witkey

    #2
    Re: Displaying Images form a Directory

    <?php
    // An Example about listing Images.
    $dir = ".";
    $odir = opendir($dir);
    while($file = readdir($odir)) {
    if(filetype($fi le) == "image/JPEG") {
    echo $file;
    }
    ?>

    Comment

    • Jameson

      #3
      Re: Displaying Images form a Directory

      Thanks for the fast response. What I'm trying to do is actually display
      the images, not list them. What I've got so far (sorry for not posting
      this earlier) is:

      <?php
      echo "<table><tr >";

      foreach(glob("u ploads/*.{jpg,JPG,jpeg ,JPEG,gif,GIF,p ng,PNG}",
      GLOB_BRACE) as $images)
      {
      echo "<td><img src=\"".$images ."\"><br/>";
      }

      echo "</tr></table>";
      ?>

      It works just fine if I want to view images in the "uploads/" folder
      (which is parallel to this script), but if I want to change it to be
      "/Volumes/jray/Pictures" it won't work.

      witkey wrote:[color=blue]
      > <?php
      > // An Example about listing Images.
      > $dir = ".";
      > $odir = opendir($dir);
      > while($file = readdir($odir)) {
      > if(filetype($fi le) == "image/JPEG") {
      > echo $file;
      > }
      > ?>[/color]

      Comment

      • Chung Leong

        #4
        Re: Displaying Images form a Directory

        Well, if the images aren't within the webroot, then the web server
        won't serve them...

        Comment

        • Jim Carlock

          #5
          Re: Displaying Images form a Directory

          "Jameson" <jameson_ray@co mcast.net> wrote:[color=blue]
          > Thanks for the fast response. What I'm trying to do is
          > actually display the images, not list them. What I've
          > got so far (sorry for not posting this earlier) is:[/color]

          Use witkey's suggestion with a modification?

          <?php
          // An Example about listing Images.
          $dir = ".";
          $odir = opendir($dir);
          while($file = readdir($odir)) {
          if(filetype($fi le) == "image/JPEG") {
          $sAltText = '"Whatever the alt text is if so required."';
          echo '<img src="' . $dir . $file . ' border="0" alt="' . $sAltText . '" /><br />' . "\n";
          }
          ?>

          Jim Carlock
          Post replies to the newsgroup.


          Comment

          • Jerry Stuckle

            #6
            Re: Displaying Images form a Directory

            Chung Leong wrote:[color=blue]
            > Well, if the images aren't within the webroot, then the web server
            > won't serve them...
            >[/color]

            Not true. You can serve them through PHP, e.g.

            <img src="showimg.ph p"...>

            And showimg.php can be something like:

            header('Content-type: image/gif');
            header('Content-length: '.filesize($img _filename));
            $file_pointer = fopen('/some/other/directory/img.gif', 'rb');
            fpassthru($file _pointer);
            fclose($file_po inter);

            The graphic can reside anywhere on the system which is accessible to the
            Apache process (not just under DocumentRoot).

            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            • Jameson

              #7
              Re: Displaying Images form a Directory

              Hi Jim:

              Thanks for the suggestion. I tried it out, but still got a blank
              screen. I think Chung might be right, because if I try "<img
              src="/Library/WebServer/Documents/uploads/door.jpg>" the web server
              won't serve them. I think the client's browser is trying to find the
              images locally on the HD.

              Maybe what I'm trying to do isn't possible. Do you think using the
              glob() function within the opendir() function might work?

              Thanks for all of the help!

              Jim Carlock wrote:[color=blue]
              > "Jameson" <jameson_ray@co mcast.net> wrote:[color=green]
              > > Thanks for the fast response. What I'm trying to do is
              > > actually display the images, not list them. What I've
              > > got so far (sorry for not posting this earlier) is:[/color]
              >
              > Use witkey's suggestion with a modification?
              >
              > <?php
              > // An Example about listing Images.
              > $dir = ".";
              > $odir = opendir($dir);
              > while($file = readdir($odir)) {
              > if(filetype($fi le) == "image/JPEG") {
              > $sAltText = '"Whatever the alt text is if so required."';
              > echo '<img src="' . $dir . $file . ' border="0" alt="' . $sAltText . '" /><br />' . "\n";
              > }
              > ?>
              >
              > Jim Carlock
              > Post replies to the newsgroup.[/color]

              Comment

              • Jim Carlock

                #8
                Re: Displaying Images form a Directory

                Hi Jameson,

                <g> I feel like I'm talking to a son... (j.k)

                I didn't test the witkey's code. It appears that filetype()
                returns "file" or "dir". Try the following.

                <?php
                // An Example about listing Images.
                $dir = ".";
                $dh = opendir($dir);
                // skip the . and ..
                $file = readdir($dh);
                $sAltText = 'Picture';
                // skip . and ..
                if ($file == ".") {
                $file = readdir($dh); $file = readdir($dh);
                do {
                if (mime_content_t ype($file)=="im age/jpeg") {
                echo '<img src="' . $dir . $file . '" border="0" alt="' . $sAltText . '" /><br />' . "\n";
                } while($file = readdir($dh));
                }
                }
                closedir($dh);
                ?>

                That should work I think. It's air code. I tested it out here
                but I'm running into a problem where the mime_content_ty pe
                is a problematic function right at the moment.

                I read that the glob function is supposed to be a better
                alternative in the PHP manual. And if you know that
                all the files in the folder are jpg's then you won't have to
                do the mime_content_ty pe test.

                My apologies about not testing witkey's code. Hope this helps.

                If anyone knows how to get the php_mime_magic. dll
                file to work on a Windows system, I could use the help.
                I uncommented the line in php.ini...

                extension=php_m ime_magic.dll

                and put the php_mime_magic. dll in the system32 folder.
                Then I stopped apache and restarted apache...

                net stop apache
                net start apache

                Thanks, much.

                Jim Carlock
                Post replies to the newsgroup.


                Comment

                • Jameson

                  #9
                  Re: Displaying Images form a Directory

                  Hi Jim:

                  Thanks for the new code. I think we're getting closer, but it still
                  doesn't seem to be working. I added the error reporting code below to
                  the file, but it doesn't tell me that anything is wrong. It's strange,
                  because I don't even see an icon from the browser saying that it can't
                  find the image. Do you think I could be missing some component of my
                  PHP installation? I know the directory path is fine, because I can get
                  to it right from the terminal on the computer.

                  ini_set("displa y_errors",true) ;
                  error_reporting (E_ALL );

                  Any more ideas? Do you think if I used the glob() function within the
                  opendir() function, it might work?

                  Thanks again for the help. I really appreciate it!

                  Comment

                  • Jim Carlock

                    #10
                    Re: Displaying Images form a Directory

                    "Jameson" <jameson_ray@co mcast.net> wrote:[color=blue]
                    > Thanks for the new code. I think we're getting closer, but it still
                    > doesn't seem to be working.[/color]

                    Hi Ray,

                    Are you working in the MacIntosh environment? I'm working on
                    with a PC. On the PC, the php.ini file typically gets found in the
                    system32 folder. I'm not sure how things work on a MacIntosh,
                    so perhaps you can explain the php.ini concepts relating to
                    MacIntosh (if that is the case) for my benefit. The php stuff is
                    very new to me.

                    I did get the following code to work here, using glob(). I really
                    like this better than having to do mime_content_ty pe stuff. And
                    so much in the PC and unix world seem very much related,
                    using file extensions to denote the contents of the file.

                    <html>
                    <head>
                    <title>List of Files</title>
                    </head>
                    <body>
                    <p><?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";
                    }
                    ?></p>
                    </body></html>
                    [color=blue]
                    > I added the error reporting code below to the file, but it doesn't
                    > tell me that anything is wrong. It's strange, because I don't even
                    > see an icon from the browser saying that it can't find the image.[/color]

                    When you view the page through the browser, take a look at the
                    source code (the HTML output). The code I posted previously
                    seems to have had a small bug in one of the lines, and I gave up
                    testing it when I couldn't get the php_mime_magic. dll to provide
                    the php_mime_conten t() function. So I don't know how the
                    Mac environment works, nor the Unix environment. The file name
                    could possibly be of a different extension in each environment. If
                    anyone else here can provide a little help here that would be
                    great.
                    [color=blue]
                    > Do you think I could be missing some component of my PHP
                    > installation? I know the directory path is fine, because I can get
                    > to it right from the terminal on the computer.[/color]

                    ini_set("displa y_errors",true) ;
                    error_reporting (E_ALL );
                    [color=blue]
                    > Any more ideas? Do you think if I used the glob() function within
                    > the opendir() function, it might work?[/color]

                    glob() is alot easier... there's no need for opendir() and readdir()
                    when using the glob function.

                    I just tested the following out and it works quite well.

                    <?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]
                    > Thanks again for the help. I really appreciate it![/color]

                    You're welcome. I'm learning as well. So thanks back at you!

                    My mime_content_ty pe() function is failing I think, because
                    the directory path for the php install folder is set to C:\php4
                    and php isn't installed there. So I guess I need to find the
                    source code for that particular DLL and modify the source
                    code. If anyone knows where to pick up the source code
                    for the php_mime_magic. dll feel free to leave a hint. It's a
                    shame (for me and others) that the people that compiled it
                    used an absolute path inside the DLL file.

                    Jim Carlock
                    Post replies to the newsgroup.


                    Comment

                    • Jim Carlock

                      #11
                      Re: Displaying Images form a Directory

                      Hello again,

                      I finally got the mime_magic stuff to work. I ended up
                      editing php.ini to get it to work.

                      The following line was commented out so all I did was
                      uncomment it.

                      extension=php_m ime_magic.dll

                      Also, I had to add a section with the following item:

                      [mime_magic]
                      mime_magic.magi cfile = "C:\path\to\PHP \install\extras \magic.mime"

                      The following code works very well, but I still like the glob()
                      function more.

                      // view the mime type
                      $rpn = "23939.jpg" ;
                      echo mime_content_ty pe($rpn);

                      returns "image/jpeg". Also, if the file is named 23939 without
                      the .jpg extension, mime_content_ty pe('23939'); also returns
                      "image/jpeg". I tested dropping the extension on a .gif file as
                      well and it correctly returned, "image/gif".

                      Hope this helps.

                      Jim Carlock
                      Post replies to the newsgroup.


                      Comment

                      • feo

                        #12
                        Re: Displaying Images form a Directory

                        header ( 'Content-Type: ' . image_type_to_m ime_type ( $img ) );
                        readfile ( $img );

                        $img is the relative path to the image file. Let's say you use this tree:

                        /directory_for_e verything
                        /directory_for_e verything/directoryforsto ringimages
                        /directory_for_e verything/directoryhttpex posed

                        If your script is stored in the last directory, then $img =
                        '../directoryforsto ringimages/nameofimage';

                        Hope it helps.

                        feo

                        "Jameson" <jameson_ray@co mcast.net> escribió en el mensaje
                        news:1136134747 .652428.106160@ g44g2000cwa.goo glegroups.com.. .[color=blue]
                        > Happy New Year, Everyone!
                        >
                        > I am trying to figure out how to display a bunch of images (mainly
                        > JPEGs, but possibly a few GIFs and PNGs as well) that are stored in a
                        > local directory on the system. I can do this with the glob() function,
                        > but I can't seem to put in a directory other than one within the
                        > webroot. For example, I can only put "/uploads" and not
                        > "/Volumes/jray/Pictures...".
                        >
                        > Any ideas how to get around this? If I can't use the glob function,
                        > it's fine, but I only want images to be displayed (and not any other
                        > file that happpens to be stored in that directory).
                        >
                        > Thanks in advance!
                        >[/color]


                        Comment

                        • Jameson

                          #13
                          Re: Displaying Images form a Directory

                          Hi Jim:

                          Thanks a million for all of the help. I think my PHP installation isn't
                          set up right, because it seems like anything that I try do do with
                          images doesn't work very well.

                          I might try making a symbolic link (like a shortcut on Windows) in the
                          directory. I know it isn't as ideal as if I could get the directory
                          parth to wrok with the glob() function, but it might be all I've got at
                          this point.

                          I'm glad you got your mime_content_ty pe to work. Sometimes we get lucky
                          and it turns out to be as simple as uncommenting a line.

                          Thanks again for all of the help!

                          Jameson

                          Jim Carlock wrote:[color=blue]
                          > Hello again,
                          >
                          > I finally got the mime_magic stuff to work. I ended up
                          > editing php.ini to get it to work.
                          >
                          > The following line was commented out so all I did was
                          > uncomment it.
                          >
                          > extension=php_m ime_magic.dll
                          >
                          > Also, I had to add a section with the following item:
                          >
                          > [mime_magic]
                          > mime_magic.magi cfile = "C:\path\to\PHP \install\extras \magic.mime"
                          >
                          > The following code works very well, but I still like the glob()
                          > function more.
                          >
                          > // view the mime type
                          > $rpn = "23939.jpg" ;
                          > echo mime_content_ty pe($rpn);
                          >
                          > returns "image/jpeg". Also, if the file is named 23939 without
                          > the .jpg extension, mime_content_ty pe('23939'); also returns
                          > "image/jpeg". I tested dropping the extension on a .gif file as
                          > well and it correctly returned, "image/gif".
                          >
                          > Hope this helps.
                          >
                          > Jim Carlock
                          > Post replies to the newsgroup.[/color]

                          Comment

                          • Jim Carlock

                            #14
                            Re: Displaying Images form a Directory

                            "Jameson" <jameson_ray@co mcast.net> wrote:[color=blue]
                            > Thanks a million for all of the help. I think my PHP installation
                            > isn't set up right, because it seems like anything that I try do
                            > do with images doesn't work very well.[/color]

                            Hi Ray,

                            If you're as curious about how things work on a Windows NT
                            system as I am about how they work on a Mac, perhaps we
                            could continue and see if we come up with something.

                            For instance, are you doing all this on a MacIntosh system? I
                            see your posting to the newsgroup using a MacIntosh. That's
                            where my questions must start.

                            X-HTTP-UserAgent:
                            Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en)
                            AppleWebKit/416.12 (KHTML, like Gecko) Safari/416.13,gzip(gfe ),gzip(gfe)

                            Apache gets installed as a "Service" on any NT system. How do
                            you start and stop a web-server on the Mac?

                            Here we either click on Start, Run then type in services.msc, or
                            we open a command prompt and type services.msc, or we open
                            a command prompt and use the following commands to start and
                            stop the service...

                            net stop Apache
                            net start Apache

                            The services.msc provides the GUI interface and opens a window
                            whereby you can right click on each listed service, then select "Stop"
                            or "Start" or get into the service "Properties " to set the Autostart
                            stuff.

                            The PHP stuff loads when Apache starts. That's the only way
                            it gets loaded. The executable, php.exe, isn't even in the PATH
                            environment variable. So the PHP install folder holds some files
                            but everything to load gets identified in the PHP.INI file. I forgot
                            to mention something about the "extension_ dir" line found in
                            PHP.INI:

                            extension_dir = "C:/WINDOWS/system32/"

                            This line positively identifies the location of the folder which
                            holds the extension files. Shortcuts to .dll files do not work on
                            a Windows system, so the actual .dll must reside in the folder
                            specified. Also, the path must be specified using the forward
                            slash, even though Windows itself uses backwards-slashes to
                            denote a path to a file.

                            So I ended up using the "net stop Apache" and "net start Apache"
                            to test things. The "net start ..." and "net stop ..." commands are
                            extremily useful on any Windows NT system (NT, 2000, XP,
                            2003).

                            So my next question is, do you employ a PHP.INI file on a Mac?

                            You are trying to get this to work on a Mac, right? If you're
                            unsure of how to answer any of the questions, let me know.
                            I'm really ignorant when it comes to MacIntosh systems, and
                            I might be able to ask some easy to answer questions if your
                            willing to answer some questions. I'm certainly eager to get
                            answers and explore this, even though no Mac sits in front of
                            me. :-)

                            Jim Carlock
                            Post replies to the newsgroup.


                            Comment

                            • Jameson

                              #15
                              Re: Displaying Images form a Directory

                              Hi Jim:

                              Sure, I'd be happy to keep looking into this. Maybe we'll end up
                              getting it to work.

                              First off, I am doing this on a Mac. Apache comes built in to OS X (I
                              believe as a service), and I installed PHP from an install package.

                              There is a PHP ini file on the Mac, although I've never had to edit it.
                              I used to run a Windows 2000 server with PHP on it, and I had to edit
                              the PHP.ini file quite a few times.

                              Restarting Apache on the Mac can either be done through the command
                              line or System Preferences. I tend to do it from the GUI, but I think
                              that "apachectl restart" will restart it from the command line.

                              There was actually something that I was wondering about your last
                              version of the script. If we take a look at it...

                              <?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";

                              }

                              ?>

                              ....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? If a directory is
                              specified in the glob() function, I believe it just defaults to the
                              directory that the script is in. Did you have a different experience on
                              your end?

                              Thanks again for sticking with this. Sometimes is is good to have an
                              understanding of how a function works across different operating
                              systems.

                              Jameson

                              Comment

                              Working...