473,614 Members | 2,268 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

My readdir and display images snippet - Thanks

Just wanted to say thanks for the posts helping me make ths work. Added
a few comments and after the readdir used a pattern match to test for
..jpg, and seems to work fine on my test setup. Maybe I should test for
gif, tiff, and png... Anyway, here's the code:
<?php
//Open images directory
$dir = opendir("images ");
//read files in the dir
while (($file = readdir($dir)) !== false)
//test to make sure jpg
if (eregi("\.jpg", $file))
{
echo "<img src='images/$file' class=\"pad1em\ ">";
}
closedir($dir);
?>

For my next trick I'm going to change the above to read a thumbs dir,
and make a link to a larger picture of the same name in images.
Shouldn't be too hard bt my last programming class was almost 20 years
ago. :) Hats off to everyone that does this for a living.

Thanks again!

Sep 12 '07 #1
9 3527

"Confused but working on it" <Po**********@w herever.comwrot e in message
news:2007091209 500416807-PostInGroups@wh erevercom...
Just wanted to say thanks for the posts helping me make ths work. Added a
few comments and after the readdir used a pattern match to test for .jpg,
and seems to work fine on my test setup. Maybe I should test for gif,
tiff, and png... Anyway, here's the code:
<?php
//Open images directory
$dir = opendir("images ");
//read files in the dir
while (($file = readdir($dir)) !== false)
//test to make sure jpg
if (eregi("\.jpg", $file))
one suggestion,

why are you using ereg? beside the fact that it is vastly more resource
intensive than preg and slower, neither is needed because substr is faster
than both.

while ...
$extension = strtolower(subs tr($file, -4));
if ($extension != '.jpg'){ continue; }
whatever you want to do with the jpg from here
end

you could also use fnmatch to pattern match for file names. you could even
use glob()...

$directory = 'images';
$extension = '*.jpg';
chdir($director y);
foreach (glob($extensio n) as $image)
{
echo '<img src="' . $directory . '/' . $image . '" class="pad1em"> ';
}

i personally prefer glob() for such things since it requires less code to
get what you want.

anyway...there' s always more than one way to skin a cat. ;^)
Sep 13 '07 #2
On 12 Sep, 17:50, Confused but working on it
<PostInGro...@w herever.comwrot e:
Just wanted to say thanks for the posts helping me make ths work. Added
a few comments and after the readdir used a pattern match to test for
.jpg, and seems to work fine on my test setup. Maybe I should test for
gif, tiff, and png... Anyway, here's the code:
<?php
//Open images directory
$dir = opendir("images ");
//read files in the dir
while (($file = readdir($dir)) !== false)
//test to make sure jpg
if (eregi("\.jpg", $file))
{
echo "<img src='images/$file' class=\"pad1em\ ">";
}
closedir($dir);
?>

For my next trick I'm going to change the above to read a thumbs dir,
and make a link to a larger picture of the same name in images.
Shouldn't be too hard bt my last programming class was almost 20 years
ago. :) Hats off to everyone that does this for a living.

Thanks again!
I think that your ereg might find files such as fred.jpgs.php

Sep 13 '07 #3

"Confused but working on it" <Po**********@w herever.comwrot e in message
news:2007091209 500416807-PostInGroups@wh erevercom...
Just wanted to say thanks for the posts helping me make ths work. Added a
few comments and after the readdir used a pattern match to test for .jpg,
and seems to work fine on my test setup. Maybe I should test for gif,
tiff, and png... Anyway, here's the code:
ok, so i've got time on my hands...after re-reading posts today, i'm on this
one again. here's something that will allow you to list any file you want
either filtering by extension or just getting everything. i've been using
the function below since glob became availabe in php. beneath it, i wrapped
up some pseudo-testing code so you could see how the function can be called
and what each param will generate for you.

have fun. ;^)

<?
function listFiles($path , $extension = array(), $combine = false)
{
if (!chdir($path)) { return array(); }
if (!$extension){ $extension = array('*'); }
if (!is_array($ext ension)){ $extension = array($extensio n); }
$extensions = '*.{' . implode(',', $extension) . '}';
$files = glob($extension s, GLOB_BRACE);
if (!$files){ return array(); }
$list = array();
foreach ($files as $file)
{
$list[] = ($combine ? $path : '') . $file;
}
return $list;
}

// sampling output
function beautify($html)
{
$html = str_replace(' :: Array', '', $html);
return $html;
}
ob_start('beaut ify');
// end sample

// test the function
$path = 'c:/inetpub/wwwroot/images';
$extensions = array('jpg', 'gif', 'tif?');

echo '<pre>director y listing for "' . $path . '"</pre>';

$images = listFiles($path );
echo '<pre>no filter, no directory :: ' . print_r($images , true) . '</pre>';

$images = listFiles($path , null, true);
echo '<pre>no filter :: ' . print_r($images , true) . '</pre>';

$images = listFiles($path , 'jpg', true);
echo '<pre>single filter :: ' . print_r($images , true) . '</pre>';

$images = listFiles($path , $extensions, true);
echo '<pre>multiple filter :: ' . print_r($images , true) . '</pre>';
?>
Sep 13 '07 #4
On 2007-09-13 07:37:21 -0700, "Steve" <no****@example .comsaid:
>
"Confused but working on it" <Po**********@w herever.comwrot e in
message news:2007091209 500416807-PostInGroups@wh erevercom...
>Just wanted to say thanks for the posts helping me make ths work. Added
a few comments and after the readdir used a pattern match to test for
.jpg, and seems to work fine on my test setup. Maybe I should test for
gif, tiff, and png... Anyway, here's the code:
<?php
//Open images directory
$dir = opendir("images ");
//read files in the dir
while (($file = readdir($dir)) !== false)
//test to make sure jpg
if (eregi("\.jpg", $file))

one suggestion,

why are you using ereg? beside the fact that it is vastly more resource
intensive than preg and slower, neither is needed because substr is
faster than both.

while ...
$extension = strtolower(subs tr($file, -4));
if ($extension != '.jpg'){ continue; }
whatever you want to do with the jpg from here
end

you could also use fnmatch to pattern match for file names. you could
even use glob()...

$directory = 'images';
$extension = '*.jpg';
chdir($director y);
foreach (glob($extensio n) as $image)
{
echo '<img src="' . $directory . '/' . $image . '" class="pad1em"> ';
}

i personally prefer glob() for such things since it requires less code
to get what you want.

anyway...there' s always more than one way to skin a cat. ;^)
Hey Steve,

I'm using eregi because I found a line on a site or in the manual that
didn't get .jpg so I fixed it to get the jpgs. Not a programmer, and
barely even a hobbyist so I do what I can to kludge some stuff
together. Maybe I can read preg, substr and glob and use one of them
but was pretty happy with the cat being skinned at all. :)
Basically I'm just writing a paragraph about and event and putting
160px images on the page with a bit of padding. Today I was going to
make this thing read thumbs and link to images... Might have to take a
nap.

Thanks for the help

Sep 14 '07 #5
On 2007-09-13 08:10:48 -0700, Captain Paralytic <pa**********@y ahoo.comsaid:
On 12 Sep, 17:50, Confused but working on it
<PostInGro...@w herever.comwrot e:
>Just wanted to say thanks for the posts helping me make ths work. Added
a few comments and after the readdir used a pattern match to test for
.jpg, and seems to work fine on my test setup. Maybe I should test for
gif, tiff, and png... Anyway, here's the code:
<?php
//Open images directory
$dir = opendir("images ");
//read files in the dir
while (($file = readdir($dir)) !== false)
//test to make sure jpg
if (eregi("\.jpg", $file))
{
echo "<img src='images/$file' class=\"pad1em\ ">";
}
closedir($dir) ;
?>

For my next trick I'm going to change the above to read a thumbs dir,
and make a link to a larger picture of the same name in images.
Shouldn't be too hard bt my last programming class was almost 20 years
ago. :) Hats off to everyone that does this for a living.

Thanks again!

I think that your ereg might find files such as fred.jpgs.php
Yo Captain! (ex-nick of mine)

So that will never be found because fred and I, well, his wife will
tell you the whole story...

Just like a million other peeps I take pictures. I load em up in my mac
in iPhoto, make an album with the corresponding name and don't include
shats that were crappy. Export to a dir called images, filezilla to my
site. Use my little code to display on a page. I tried iWeb to make
pages, gallery databases done in php, and holy cow way to complicated.
Just easier to manage everything in directories by some kind of topic.

So is there a way for your exampe to happen?

Thanks for your input,
Ron

Sep 14 '07 #6
On 2007-09-13 09:07:17 -0700, "Steve" <no****@example .comsaid:
>
"Confused but working on it" <Po**********@w herever.comwrot e in
message news:2007091209 500416807-PostInGroups@wh erevercom...
>Just wanted to say thanks for the posts helping me make ths work. Added
a few comments and after the readdir used a pattern match to test for
.jpg, and seems to work fine on my test setup. Maybe I should test for
gif, tiff, and png... Anyway, here's the code:

ok, so i've got time on my hands...after re-reading posts today, i'm on
this one again. here's something that will allow you to list any file
you want either filtering by extension or just getting everything. i've
been using the function below since glob became availabe in php.
beneath it, i wrapped up some pseudo-testing code so you could see how
the function can be called and what each param will generate for you.

have fun. ;^)

<?
function listFiles($path , $extension = array(), $combine = false)
{
if (!chdir($path)) { return array(); }
if (!$extension){ $extension = array('*'); }
if (!is_array($ext ension)){ $extension = array($extensio n); }
$extensions = '*.{' . implode(',', $extension) . '}';
$files = glob($extension s, GLOB_BRACE);
if (!$files){ return array(); }
$list = array();
foreach ($files as $file)
{
$list[] = ($combine ? $path : '') . $file;
}
return $list;
}

// sampling output
function beautify($html)
{
$html = str_replace(' :: Array', '', $html);
return $html;
}
ob_start('beaut ify');
// end sample

// test the function
$path = 'c:/inetpub/wwwroot/images';
$extensions = array('jpg', 'gif', 'tif?');

echo '<pre>director y listing for "' . $path . '"</pre>';

$images = listFiles($path );
echo '<pre>no filter, no directory :: ' . print_r($images , true) . '</pre>';

$images = listFiles($path , null, true);
echo '<pre>no filter :: ' . print_r($images , true) . '</pre>';

$images = listFiles($path , 'jpg', true);
echo '<pre>single filter :: ' . print_r($images , true) . '</pre>';

$images = listFiles($path , $extensions, true);
echo '<pre>multiple filter :: ' . print_r($images , true) . '</pre>';
?>
Steve,

Wow, way too much time on your hands. :)

My first goal was to get my pics to display, then get rid of the . and
..., now I will fix the code to the example you gave before if you say
it's faster. Then,(light drumroll...), I want to read everything in
thumbs and create my output so I get the thumbto link to a larger pic
in images. Some sort of anchor tag thing. THAT would be cool to me.
Then I can rewrite about 20 pages, export 20 albums as thumbs and 20
albums as images... :)

I'm going to make my anchor tag...

thx..ron

Sep 14 '07 #7
On 2007-09-13 07:37:21 -0700, "Steve" <no****@example .comsaid:
>
"Confused but working on it" <Po**********@w herever.comwrot e in
message news:2007091209 500416807-PostInGroups@wh erevercom...
>Just wanted to say thanks for the posts helping me make ths work. Added
a few comments and after the readdir used a pattern match to test for
.jpg, and seems to work fine on my test setup. Maybe I should test for
gif, tiff, and png... Anyway, here's the code:
<?php
//Open images directory
$dir = opendir("images ");
//read files in the dir
while (($file = readdir($dir)) !== false)
//test to make sure jpg
if (eregi("\.jpg", $file))

one suggestion,

why are you using ereg? beside the fact that it is vastly more resource
intensive than preg and slower, neither is needed because substr is
faster than both.

while ...
$extension = strtolower(subs tr($file, -4));
if ($extension != '.jpg'){ continue; }
whatever you want to do with the jpg from here
end

you could also use fnmatch to pattern match for file names. you could
even use glob()...

$directory = 'images';
$extension = '*.jpg';
chdir($director y);
foreach (glob($extensio n) as $image)
{
echo '<img src="' . $directory . '/' . $image . '" class="pad1em"> ';
}

i personally prefer glob() for such things since it requires less code
to get what you want.

anyway...there' s always more than one way to skin a cat. ;^)
Here's how I changed the line:

echo "<a href='images/$file'><img src='thumbs/$file'
class=\"pad1em\ "></a>";

My originals were 1600x1200 so I did a bew export to thumbs at 100x75
and another to images at 400x300. Freakin fantastic! Had to refresh the
page as the old thumb was showing but very cool. Will play with size I
shoot at and then corresponding thumbs and bigger versions. Like 1024
wide makes nice thumbs at 128, click to see a 512 or 640 bersion.

Before I start hacking your code in from above, any reason the
following wont work?
if ($extension != '.jpg' or != 'gif' or != '.tif' ){ continue; }
Or use the whole expression?

Thanks again!
ron

Sep 14 '07 #8
On Fri, 14 Sep 2007 23:42:46 +0200, Confused but working on it
<Po**********@w herever.comwrot e:
On 2007-09-13 08:10:48 -0700, Captain Paralytic <pa**********@y ahoo.com
said:
>On 12 Sep, 17:50, Confused but working on it
<PostInGro...@ wherever.comwro te:
>>Just wanted to say thanks for the posts helping me make ths work. Added
a few comments and after the readdir used a pattern match to test for
.jpg, and seems to work fine on my test setup. Maybe I should test for
gif, tiff, and png... Anyway, here's the code:
<?php
//Open images directory
$dir = opendir("images ");
//read files in the dir
while (($file = readdir($dir)) !== false)
//test to make sure jpg
if (eregi("\.jpg", $file))
{
echo "<img src='images/$file' class=\"pad1em\ ">";
Hmm, are you sure about the class name? I'd say that on a new layout with
the same HTML 'pad1em' could end up making very little sense. A golden
rule of css is that a classname should reflect what something is, not how
it is _displayed. A more suitable name could be something like
'imagepreview' or the like.
>>}
closedir($dir );
?>
For my next trick I'm going to change the above to read a thumbs dir,
and make a link to a larger picture of the same name in images.
Shouldn't be too hard bt my last programming class was almost 20 years
ago. :) Hats off to everyone that does this for a living.
Thanks again!
I think that your ereg might find files such as fred.jpgs.php

Yo Captain! (ex-nick of mine)

So that will never be found because fred and I, well, his wife will tell
you the whole story...

Just like a million other peeps I take pictures. I load em up in my mac
in iPhoto, make an album with the corresponding name and don't include
shats that were crappy. Export to a dir called images, filezilla to my
site. Use my little code to display on a page. I tried iWeb to make
pages, gallery databases done in php, and holy cow way to complicated.
Just easier to manage everything in directories by some kind of topic.

So is there a way for your exampe to happen?
Oh yeah.
(Just make sure the file is a proper image directly after an upload.
Examining extention or mime-type is no good. I'm quite happy to us
getimagesize() for this purpose, as it would return false if it couldn't
interpret a file as an image.)

And to display only files _ending_ in jp(e)g, I'd use
preg_match('/\.jpe?g$/i',$string);
--
Rik Wasmus
Sep 15 '07 #9
On 2007-09-14 19:30:34 -0700, "Rik Wasmus" <lu************ @hotmail.comsai d:
>>>>
echo "<img src='images/$file' class=\"pad1em\ ">";

Hmm, are you sure about the class name? I'd say that on a new layout wit h
the same HTML 'pad1em' could end up making very little sense. A golden
rule of css is that a classname should reflect what something is, not ho w
it is _displayed. A more suitable name could be something like
'imagepreview' or the like.
>>

Oh yeah.
(Just make sure the file is a proper image directly after an upload.
Examining extention or mime-type is no good. I'm quite happy to us
getimagesize() for this purpose, as it would return false if it couldn't
interpret a file as an image.)

And to display only files _ending_ in jp(e)g, I'd use
preg_match('/\.jpe?g$/i',$string);
--
Rik Wasmus
Rik,

Thanks for responding. In this case the class name is what it is.
..pad1em{paddin g: 1em;}

Was just messing with some spacing and will rename if I add some pretty
decorations. Any suggestions for this class?

thx..ron

Sep 16 '07 #10

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

Similar topics

8
4614
by: Oliver | last post by:
Hi, I wrote earlier this day about this problem but thought it had something to do with images. However, I found out that for some reason the readdir function is not working properly on my Suse 9.1 server. I can only read a random number of files and directories per folder. When I repeat the command for the same folder I get the same output. If I use the readdir test script on an older machine everything works fine.
7
13857
by: Ask Josephsen | last post by:
Hi NG In wich order does "readdir()" read files from the disc? I've got an image folder with images in the format "00001.jpg", "00002.jpg" etc. It seems "readdir()" read the lowest first, but is it certain? An alternative is to read the files into an array and sort them, does anyone have any experience with this? Should I use the "SORT_REGULAR", "SORT_NUMERIC" or "SORT_STRING"?
1
1400
by: tmb | last post by:
Gee, it has been a few years since I've done ASP. I wonder if anyone would save me some time and share a script for doing this that I could use as a model. I have y pic's on the server and each time someone visits the site I want to display x of them randomly. thanks for any help.
26
2419
by: Yeah | last post by:
I have a web site which changes the header logo based on the upcoming holiday. For example, from December 10th to 25th, XMAS.JPG is displayed. From October 20th to 31st, HALLWEEN.JPG is displayed. Etc. etc. If today's date is not near a holiday, then the default LOGO.JPG is displayed. A while back, I was looking for a JavaScript that does this automagically. But each snippet I found traditionally displays the default logo for a...
5
5025
by: Peter Lapic | last post by:
I have to create a image web service that when it receives an imageid parameter it will return a gif image from a file that has been stored on the server. The client will be an asp.net web page that calls the web service to render a vertical strip of images. After doing some research I am unable to find some vb.net code that can assist in what I want to achieve. The closest thing I found was
4
5607
by: drew197 | last post by:
I am a newbie. I am editing someone elses code to make it compatible with Firefox and Safari. In IE, when you click on the proper link, a block of text is shown in a nice paragraph form. But, in FireFox and Safari it appears as a narrow column of text with only 2-3 words per line. Here is the code: function showAll()
4
44932
by: MZ | last post by:
Hello! I have written such function... Unfortunately images are not sorted by filename and I don`t know why. I have such files placed in subfolder: 2005_11_17_koncert_Riverisde_w_Poznaniu_1.jpg 2005_11_17_koncert_Riverisde_w_Poznaniu_2.jpg
2
2471
by: kirstenkirsche | last post by:
Hi guys.... i know this question has been asked already and I found a few answers but it would be soo great if anyone could help me with this script, which just browses images on a webpage in an image folder on a server. the script so far is working (a friend gave it to me): http://dev.perfectday.gb.com/tom/chiaraweb/photo.php the number on the images are the filenames. I want to order the images by filenames. the only thing is the...
3
3883
anfetienne
by: anfetienne | last post by:
Hi, What would be the best way or how should i say do i sort images that are named numerically in order? this is my original code, i was told that it should sort perfecty fine with what ive got but it doesn't.....its all mixed up <?php $path = "upload/$random_digit/images/"; // path to the directory to read ( ./ reads the dir this file is in)
0
8130
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
8623
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...
1
8275
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5538
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4050
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...
0
4121
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2566
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
1
1745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1423
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.