473,671 Members | 2,550 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!

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

Jan 1 '06 #2
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:
<?php
// An Example about listing Images.
$dir = ".";
$odir = opendir($dir);
while($file = readdir($odir)) {
if(filetype($fi le) == "image/JPEG") {
echo $file;
}
?>


Jan 2 '06 #3
Well, if the images aren't within the webroot, then the web server
won't serve them...

Jan 2 '06 #4
"Jameson" <ja*********@co mcast.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($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.
Jan 2 '06 #5
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.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.
js*******@attgl obal.net
=============== ===
Jan 2 '06 #6
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*********@co mcast.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($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.


Jan 2 '06 #7
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.
Jan 3 '06 #8
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!

Jan 3 '06 #9
"Jameson" <ja*********@co mcast.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_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>
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_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.
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?
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";
}
?>
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_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.
Jan 3 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2325
by: Vinay | last post by:
Hi All: I have a small application that stores images either in the database or as files (depending on the user preference). I'm in the process of providing a web interface to this application. 1. If the images are stored in the DB then, every an image is requested, it will need to be pulled out and a temp file created and then displayed? What is the best way to do this?
0
1473
by: Matt | last post by:
Hi I'm have difficulty displaying images that are stored in a MySQL database. I've tried using the odbc.net provider, the ByteFX provider and the MySQLDriverCS one but can't seem to get the image to display on the screen. I've tried searching on the net but can only find examples on how to do it on SQL Server and havent had any luck converting them to work with MySQL. I thought I had to put the data into a byte variable then use
3
2357
by: CLEAR-RCIC | last post by:
I have several images i want to display in an ASP.Net application. The images are being passed to me in binary format from another application. Is there a good way to write them directly to an HMTL page without having to save them to the server and create a URL to the virtual directory? FYI: I currently am doing this with just single images. I do a Response.BinaryWrite(byte) to display the one image. The advantage is that I never...
5
1779
by: kbrad | last post by:
I have an Access database set up with a number of text fields and a hyperlink field which references a different image per record. I am trying write ASP code to retrieve allt his data and images onto a web page. I have the data but the images are causing a problem. The code I am using is as follows: For the data: <TD><P><FONT COLOR="#000000"><% Response.Write rsRecordset("Fieldname") %></FONT></TD> This is fine. For the images:
10
5749
by: gnewsgroup | last post by:
I've googled and tried various approaches, but could not resolve this problem. The article at MSDN: Displaying Images in a GridView Column only presents a simple case where all data (including the images) of the gridview come from a single table/datasource. Here is my situation. In my web application, I need to display customer bills info in a gridview. Customer names and contact info are from the Customer table.
5
11892
by: jearnizck | last post by:
hi everyone! is there someone who can help me, i am a beginner in php, i have a code that displaying images from my images directory but i want it to be in a table style...my output of my code is displying in a single row...i want it to display a limit of 4 columns per row...here's my code...please help me! thanks in advance! <?php // Define the full path to your folder, shouldn't need changing $path = "./"; // Open the...
14
2110
by: ashraf02 | last post by:
i used a code from a website that allows you to display images. however everything works fine from storing the image to the database but it does not display the image. the following code is the one i have used. Storing the images: <HTML> <HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD> <BODY>
3
1701
by: Hataf | last post by:
Hi! I am having a problem in displaying images from database. This the code gallery.php <?php session_start(); if(isset($_SESSION)){} //die('ERROR:'.mysql_error());
0
8483
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8402
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8927
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8825
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6237
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2819
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2062
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.