By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,584 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,584 IT Pros & Developers. It's quick & easy.

Displaying Images form a Directory

P: n/a
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!

Jan 1 '06 #1
Share this Question
Share on Google+
15 Replies


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

Jan 1 '06 #2

P: n/a
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;
}
?>


Jan 2 '06 #3

P: n/a
Well, if the images aren't within the webroot, then the web server
won't serve them...

Jan 2 '06 #4

P: n/a
"Jameson" <ja*********@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.
Jan 2 '06 #5

P: n/a
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.
js*******@attglobal.net
==================
Jan 2 '06 #6

P: n/a
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" <ja*********@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.


Jan 2 '06 #7

P: n/a
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.
Jan 3 '06 #8

P: n/a
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!

Jan 3 '06 #9

P: n/a
"Jameson" <ja*********@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.
Jan 3 '06 #10

P: n/a
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_mime_magic.dll

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

[mime_magic]
mime_magic.magicfile = "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_type($rpn);

returns "image/jpeg". Also, if the file is named 23939 without
the .jpg extension, mime_content_type('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.
Jan 4 '06 #11

P: n/a
feo
header ( 'Content-Type: ' . image_type_to_mime_type ( $img ) );
readfile ( $img );

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

/directory_for_everything
/directory_for_everything/directoryforstoringimages
/directory_for_everything/directoryhttpexposed

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

Hope it helps.

feo

"Jameson" <ja*********@comcast.net> escribió en el mensaje
news:11**********************@g44g2000cwa.googlegr oups.com...
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!

Jan 4 '06 #12

P: n/a
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_type 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:
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_mime_magic.dll

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

[mime_magic]
mime_magic.magicfile = "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_type($rpn);

returns "image/jpeg". Also, if the file is named 23939 without
the .jpg extension, mime_content_type('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.


Jan 5 '06 #13

P: n/a
"Jameson" <ja*********@comcast.net> wrote:
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.


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.
Jan 5 '06 #14

P: n/a
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>filename:</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

Jan 6 '06 #15

P: n/a
"Jameson" <ja*********@comcast.net> wrote:
If we take a look at it...
<?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";
}
?>
...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?
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";
If a directory is specified in the glob() function, I believe it
just defaults to the directory that the script is in.
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>filename:</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\system32\ 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.
Thanks again for sticking with this. Sometimes is is good
to have an understanding of how a function works across
different operating systems.


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.
Jan 6 '06 #16

This discussion thread is closed

Replies have been disabled for this discussion.