Displaying Images form a Directory

This is a discussion on Displaying Images form a Directory within the PHP Language forums, part of the PHP Programming Forums category; Happy New Year, Everyone! I am trying to figure out how to display a bunch of images (mainly JPEGs, but ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-01-2006
Jameson
 
Posts: n/a
Default 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!

Reply With Quote
  #2 (permalink)  
Old 01-01-2006
witkey
 
Posts: n/a
Default Re: Displaying Images form a Directory

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

Reply With Quote
  #3 (permalink)  
Old 01-02-2006
Jameson
 
Posts: n/a
Default 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("uploads/*.{jpg,JPG,jpeg,JPEG,gif,GIF,png,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:
> <?php
> // An Example about listing Images.
> $dir = ".";
> $odir = opendir($dir);
> while($file = readdir($odir)){
> if(filetype($file) == "image/JPEG") {
> echo $file;
> }
> ?>


Reply With Quote
  #4 (permalink)  
Old 01-02-2006
Chung Leong
 
Posts: n/a
Default Re: Displaying Images form a Directory

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

Reply With Quote
  #5 (permalink)  
Old 01-02-2006
Jim Carlock
 
Posts: n/a
Default Re: Displaying Images form a Directory

"Jameson" <jameson_ray@comcast.net> wrote:
> 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:


Use witkey's suggestion with a modification?

<?php
// An Example about listing Images.
$dir = ".";
$odir = opendir($dir);
while($file = readdir($odir)){
if(filetype($file) == "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.


Reply With Quote
  #6 (permalink)  
Old 01-02-2006
Jerry Stuckle
 
Posts: n/a
Default Re: Displaying Images form a Directory

Chung Leong wrote:
> Well, if the images aren't within the webroot, then the web server
> won't serve them...
>


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

<img src="showimg.php"...>

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_pointer);

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@attglobal.net
==================
Reply With Quote
  #7 (permalink)  
Old 01-02-2006
Jameson
 
Posts: n/a
Default 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:
> "Jameson" <jameson_ray@comcast.net> wrote:
> > 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:

>
> Use witkey's suggestion with a modification?
>
> <?php
> // An Example about listing Images.
> $dir = ".";
> $odir = opendir($dir);
> while($file = readdir($odir)){
> if(filetype($file) == "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.


Reply With Quote
  #8 (permalink)  
Old 01-03-2006
Jim Carlock
 
Posts: n/a
Default 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_type($file)=="image/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_type
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_type 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_mime_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.


Reply With Quote
  #9 (permalink)  
Old 01-03-2006
Jameson
 
Posts: n/a
Default 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("display_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!

Reply With Quote
  #10 (permalink)  
Old 01-03-2006
Jim Carlock
 
Posts: n/a
Default Re: Displaying Images form a Directory

"Jameson" <jameson_ray@comcast.net> wrote:
> Thanks for the new code. I think we're getting closer, but it still
> doesn't seem to be working.


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_type 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>filename:</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>

> 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.


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_content() 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.

> 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("display_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?


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>filename:</i> <b>$file</b>, <i>filetype:</i> <b>" . filetype($file) . "</b><br />\n";
echo '<img src="' . $file . '" border="0" alt="$sAltText" /><br />' . "\n";
}
?>

> Thanks again for the help. I really appreciate it!


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

My mime_content_type() 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.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 11:50 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0