473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

showing images from a directory

hey guys. okay... this may seem confussing to you but im in desperate
need of help!! basically i have a list of images in a directory called
'pictures'.. and i was hoping to show them on a web page by the
following code:

<?php

$handle = opendir('pictures');

if($handle) {

while(false !== ($file = readdir($handle))) {

if(preg_match("/\w+(.jpg)/",$file)) {

print"<a href='pictures'>"$file"</a>";
print "$file <br>";

}

}
closedir($handle);
}
?>

but the code doesnt work and ive tried so hard to sort it out.. any one
able to help????

Dec 13 '05 #1
7 1633
Change to:

print"<a href='pictures/$file'>$file</a>";

If you want to display the image then you have to do something like

print "<img src='pictures/$file'>";

Dec 13 '05 #2
patrick_woflian wrote:
[...] i have a list of images in a directory called
'pictures'.. and i was hoping to show them on a web page by the
following code: <snip> but the code doesnt work and ive tried so hard to sort it out.. any one
able to help????


I've reformatted your code which will let you find one error easily
and added a few comments (the ### lines)

<?php
$handle = opendir('pictures');
if ($handle) {
while (false !== ($file = readdir($handle))) {

### this will match foto5jpg without extension;
### probably you will not have such a filename in the directory
### but you may want to remove the () and escape the .
if (preg_match("/\w+(.jpg)/", $file)) {

### when someone clicks the filename do you want
### them to go to the "pictures" directory?
### AND check your quotes
print "<a href='pictures'>"$file"</a>";

### Why are you printing $file twice?
print "$file <br>";
}
}
closedir($handle);
}
?>

Dec 13 '05 #3

patrick_woflian wrote:
hey guys. okay... this may seem confussing to you but im in desperate
need of help!! basically i have a list of images in a directory called
'pictures'.. and i was hoping to show them on a web page by the
following code:
You gots to look at the html that is produced by this code, and see if
it makes sense: <?php

$handle = opendir('pictures');

if($handle) {

while(false !== ($file = readdir($handle))) {

if(preg_match("/\w+(.jpg)/",$file)) {

print"<a href='pictures'>"$file"</a>";
So where is this link supposed to go? Maybe you wanted "<a
href='pictures.htm'>"?

print "$file <br>";


Did you want this to print the actual image, or just the name of it?
If you want to see the image, you would do like:
print "<img src='myImageDir/$file'>";

--
juglesh

Dec 13 '05 #4
Mara Guida wrote:
### this will match foto5jpg without extension;
### probably you will not have such a filename in the directory
### but you may want to remove the () and escape the .
if (preg_match("/\w+(.jpg)/", $file)) {


Just one more thing ...

preg_match("/\w+(.jpg)/", $file)

will also match "rejpg.doc"

Dec 13 '05 #5
cheers for all your help.. the code works now.. but it only loads two
images despite there being more in the directory.. any ideas why this
is? im looking to post about 7 pictures... heres the new code:

<html>

<head>

<title>

My wonderfully great pictures

</title>

</head>
<body>
<BODY BGCOLOR = "green">
<pre>
<h1> Photo Gallery </h1>

Click on the link to view image in separate window
</pre>

<?php

$handle = opendir('pictures');

if($handle) {

while(false !== ($file = readdir($handle))) {

if(preg_match("/\w+(.jpg)/",$file)) {

print"<a href='pictures/$file'>$file</a>";


print "$file <br>";

}

}
closedir($handle);
}
?>

</body>
</html>

Dec 13 '05 #6
Mara Guida wrote:
I've reformatted your code which will let you find one error easily


Oops, sorry. Ignore this sentence.

Dec 13 '05 #7
Don't be a dope--use glob():

$files = glob("pictures/*.{jpg,JPG,jpeg,JPEG,gif,GIF}", GLOB_BRACE);
foreach($files as $file) {
$filename = basename($file);
echo "<a href='$file'>$filename</a><br>";
}

http://fi.php.net/glob/

P.S. The reason you're not seeing the other pics is probably because
you're using a case-sensitive regexp.

Dec 14 '05 #8

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

Similar topics

8
by: lawrence | last post by:
Under the domain publicpen.com I've several dozen sites in subdiretories, such as www.publicpen.com/honenbeger. I've no trouble with any of these sites. But under one, which I put in yesterday,...
1
by: lewindha | last post by:
Hi guys I am new to ASP and .Net. I am working a web app where some Crystal Reports will be incorporated. Right now I am trying to get a simple report with a bar graph to show up in a browser....
2
by: Alex | last post by:
I have a web application hosted on the server's F:\www directory. I have images stored on the file system at E:\Images. I have the website shared thru IIS pointing to the F:\www directory. 1.)...
15
by: Jameson | last post by:
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....
12
by: John Kotuby | last post by:
Hi all, Maybe this is a simple problem found in ASP.NET 2.0 course 101, but I must have missed it. When I create a page in Visual Web Developer and use URLs like "/images/picture.gif " or a link...
6
by: MaiyaHolliday | last post by:
Hello, I've recently installed apache on a new computer, and cannot figure out why my site will not process any includes. (it was working on my old one) There are no errors on the page such as...
2
by: wstsoi | last post by:
hi I have to read images from spreadsheet, is it possible to do with php?
7
by: Jurjen de Groot | last post by:
I'm developping a web-app using VS2008 (Pro edition) on a Vista Ultimate machine .. For some reason images aren't shown, the only thing shown is the little square with the circel, triangle and...
3
by: David C | last post by:
I have a new asp.net application with a virtual directory that holds photos (jpg files). I am trying to display them in a DataList and all I get is the small red X in upper left of each img...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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...

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.