Also since we are using the command ucfirst() inside the function there is no need to capitolize the first letter of each sentence.
Enjoy ;)
Main Topics
Browse All TopicsHi guys,
My php is limited so any help/examples are much appreciated.
I have a directory that holds several images for a section as follows:
/literature/
literature1.jpg
literature2.jpg
literature3.jpg
literature4.jpg
literature5.jpg
literature6.jpg
etc ..
What I want is to display "literature1.jpg" with some "descriptive text" on a page.
So the html output would look like:
<p>Here is the text for the following image</p>
<img src="/images/literature/li
Now I want these to all work with the same template. So it will load the first by default and then each image/description will be represented by pagination like so:
1, 2, 3, 4, 5, 6, etc
So each number will change the query in the url.
Any ideas on how best to approach this. I have opted against using a database so it is territory I am unfamiliar with.
Thanks.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
A purely php method for what? To include the text? Hard-coding the text into a php file is pretty much the worst way you could do it, but if you're intent on doing that, you can simple store it in an array.
$items[0] = "some text";
$items[1] = "some more text";
etc...
But I strongly recommend against this.
Well, no. You could have an ini file for each image which would have the attributes for the image (caption, sizes unless they're all the same size and any other information needed for them). Or you could even include a base64 encoded version of the image in the ini file. A database is just an alternate method of storage, the concept behind it is still the same even if you go with the filesystem method, so you still should think in the same terms.
One way you can do it is to have the script in the document root directory, and the ini files (or even php files) in each subdirectory.
If you opt for php, you can use a format like this for the text. Name it captions.php:
<?php
$caption['literature1.jpg'
$caption['literature2.jpg'
$caption['literature3.jpg'
...
?>
Then, in the doc root, the script should accept a URL like this:
/show.php?dir=literature&i
The image parameter should be optional, too. The script could work like this:
<?php
$dir = $_GET['dir'];
$image = $_GET['image'];
// validate directory
if (preg_match("#(\.|/)#", $dir)) die('dots and slashes in dir disallowed');
if (!is_dir($dir)) die('bad directory');
include($dir."/captions.ph
// load all the images into an array
$dh = opendir($dir);
while($d = readdir($dh)) {
if (!preg_match("#^\.#",$d)) $images[] = $d;
}
// if there's no image specified, use a default
if (!$image) $image=$images[0];
if (preg_match("#/#", $image)) die('slashes in image disallowed');
if (!file_exists($image)) die("image does not exist");
// draw the navigation
for($i=0; $i < count($images); $i++)
{
$img = $images[$i];
if ($img != $image)
echo "<a href=?dir=$dir&image=$img>
else
echo "$i";
if ($i < count($images) - 1) echo ", ";
}
echo "<img src='$dir/$image'>";
echo $captions[$image];
// maybe the images array should be sorted
?>
All of this, including my own suggestions, does add overhead, though. This is basically the reason why database servers exist. To offload the overhead of the alternative methods onto a system geared primarily towards it. Also, when all is said and done, the time and effort of coding it in a way that uses the filesystem instead is on the same level of difficulty as that which would have been used to utilize a database. And likely longer as the code to retrieve information from a database for several database systems is already included in php whereas to do it with any filesystem method, you're going to have to cobble together the pieces dependent on the way you store the information.
Well, no...the database method isn't necessarily slower. It 'can' be slower, but it depends on a number of factors, not least of which is the makeup of the server it's housed on. With a dedicated server with fast scsi hard drives and a small number in the data set, yes, it can be faster to use the filesystem. It's likely you're on a shared hosting server where the filesystem of the server is already heavily used. This can easily shift the balance of speed. The only way to actually determine that is to test it. There's no situation in which it's a 'given'. Traversing directories can also add overhead that can shift the balance. Every part of the equation counts. Accessing a file in a subdirectory can often times be slower than accessing a file in a subdirectory. For permission reasons if nothing else (and it takes as much time to verify the permissions be it set right or wrong).
Add these two lines to the program, around where the img tag is generated.
$nextimage = $images[$i+1];
if ($nextimage) echo "<img src='$dir/$nextimage' width=1 height=1>";
And adjust your layout so you can hide this image somewhere on the page.
--------------------------
Also, I realized that I didn't need to do the directory traversal. That was extraneous.
Replace:
// load all the images into an array
$dh = opendir($dir);
while($d = readdir($dh)) {
if (!preg_match("#^\.#",$d)) $images[] = $d;
}
With:
$images = array_keys($captions);
I forgot that the filenames are in the caption's keys.
--------------------------
As for the DB versus file... The DB way is usually easier, but there is some overhead in learning it. The "file" way depends on naming conventions and whatnot, but, on the other hand, most programmers know the filesystem calls anyway (and if they don't, they eventually will). Performance-wise, it's important to remember that accessing the db on a shared server results in a file-open, unless you're a very busy site. The DB runs on top of the file system.
There is an interesting semantic differences between a file hierarchy and sets of tables. One is a hierarchy, and the other is tabular. If you want to create a site that has hierarchy and accesses files, you might write less code with the file method. If you want to simulate "sets" or "tables", then doing it in a DB will map more closely.
I'm not an anti-DB bigot or anything like that. I appreciate a complex query as much at the next programmer. Usually, I try to push all the application logic into the database. I just think when the data is tiny and the queries are simple, it's easier to use files. This goes double for languages like PHP that have good support for arrays and associative arrays, because they can sometimes "act like" tables. My usual policy is if I have to do a table join, I'll move it into a DB.
Insomnia strikes again...
I tested this script. It's got some mangled URLs that aren't really "secret". They'll just prevent some people from seeing the filenames and directories. It does the image preload. There can never be too many gallery scripts.
<?php
###### user settings ##########################
$defaultDir = 'img'; // this must be set
##########################
$args = array_keys($_GET);
$arg = $args[0];
list($dir,$image) = unserialize(base64_decode(
// set a default dir
if (!$dir) $dir = 'img';
// validate directory
if (preg_match("#(\.|/)#", $dir)) die('dots and slashes in dir disallowed');
if (!is_dir($dir)) die('bad directory');
include($dir."/captions.ph
// load all the images into an array
$images = array_keys($captions);
// if there's no image specified, use a default
if (!$image) $image=$images[0];
if (preg_match("#/#", $image)) die('slashes in image disallowed');
if (!file_exists("$dir/$image
// draw the navigation
$nav = '';
for($i=0; $i < count($images); $i++)
{
$img = $images[$i];
$arg = base64_encode(serialize(ar
if ($img != $image)
$nav .= "<a href=?$arg>".($i+1)."</a>"
else
$nav .= ($i+1).' ';
if ($i < count($images) - 1) $nav .= ', ';
}
$nextImage = $images[$i];
if (!$nextimage) $nextImage = $images[$i-1];
?>
<html>
<head>
<style type="text/css">
body {
background-color: #eee;
font-size: 80%;
font-family: "HelveticaNeue","Arial","V
}
.nav {
text-align: center;
padding: 5px;
background-color: #ddd;
}
.content {
text-align: center;
padding: 30px;
background-color: white;
height: 100%;
}
.caption {
padding: 10px;
}
</style>
<body>
<div class="nav">
<?=$nav?>
</div>
<div class="content">
<div class="image">
<img src='<?=$dir?>/<?=$image?>
</div>
<div class="caption">
<?=$captions[$image]?>
</div>
</div>
<img src="<?=$dir?>/<?=$nextIma
</body>
</html>
Well, it's not quite as good, because it will try to load the next image simultaneously. A really decent javascript preloader would wait until the first image loaded, then try to start loading the second. You'd need to use setInterval() to run a function that would do the following:
check that the main image is loaded
if it is, then check if the next image is loaded into the second img tag's src
if it is not, then set the second img tag's src to the next image.
Also alter the script so that the image tags have IDs, and the second image tag should load a the main photo, not the next photo.
Business Accounts
Answer for Membership
by: gemdeals395Posted on 2006-12-10 at 12:18:25ID: 18111551
Wow, without using a database I guess you could rename the image names like so:
ollowing_i mage.jpg
following_ image^.jpg ';
Here_is_the_text_for_the_f
Then when you read the directory you get the names of the files into variables then alter the text based on the way you named it like in the following we replace underscores with spaces and the up arrao shift+6 with a period. Now by formatting how you name each image you can get the results your after with out a database ;)
function formatText($image) {
$text = explode('.', $image);
$text = str_replace("_", " ", $text[0]);
$text = str_replace("^", ".", $text);
$text = ucfirst($text);
return $text;
}
$image = 'here_is_the_text_for_the_
$text = formatText($image);
echo("<p>$text</p>");