•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 427,669 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,279 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1683 | Replies: 2
![]() |
•
•
Join Date: Sep 2006
Location: Broken Arrow, OK
Posts: 31
Reputation:
Rep Power: 3
Solved Threads: 0
Hello gang,
Here I am with another Newbie question.
I'm creating a photo gallery for my Web site.
Right now I use a series of links to create the gallery:
My Linux host will already make the thumbnails from images I upload, and I've styled the links so they appear the way I want them to and it works fine.
I would like to be able to generate these links using a php script instead of having to make the above by hand. I'd like it to read the filenames in the gallery folder and create the links.
I looked at the oepndir command, but I don't know how I can make it create a link. I'm just plain confused.
Thanks for anyhelp you can give me.
Peace
Here I am with another Newbie question.
I'm creating a photo gallery for my Web site.
Right now I use a series of links to create the gallery:
<a href="./images/gallery/photo1.php"><img src="./images/gallery/thumbs/tn_photo1.php"></a> <a href="./images/gallery/photo2.php"><img src="./images/gallery/thumbs/tn_photo2.php"></a> <a href="./images/gallery/photo3.php"><img src="./images/gallery/thumbs/tn_photo3.php"></a>
My Linux host will already make the thumbnails from images I upload, and I've styled the links so they appear the way I want them to and it works fine.
I would like to be able to generate these links using a php script instead of having to make the above by hand. I'd like it to read the filenames in the gallery folder and create the links.
I looked at the oepndir command, but I don't know how I can make it create a link. I'm just plain confused.
Thanks for anyhelp you can give me.
Peace
Hi,
What you want to do is:
Open a directory handle and read each element in the directory.
When you read a directory, it returns all the elements in the directory.
Some of these are files, and some folders and it will also return two "virtual" folders "." and "..".
When you read a directory element usign readdir(/* opendir handle */) it will return the element name, such as the file name, or the directory name.
What you need is to get the file name, and create a link with it.
Example:
Here's an example page that should do what you want. In the example all files in the subdirectory "imgs" will be read and displayed by the php script.
[PHP]
/<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Show Images in Folder (simple gallery)</title>
<style type="text/css">
.img {
border: 1px solid #ccc;
float: left;
text-align: center;
padding: 2px;
}
.img_label {
border-bottom: 1px dotted #eee;
display: block;
}
.sep {
clear: both;
}
</style>
</head>
<body>
<?php
/**
* Read files in a directory
* @author digital-ether at fijiwebdesign.com
*
* @param string directory path
*/
function read_dir_elements($dir_path, $func) {
if ($dh = opendir($dir_path)) {
while ($el = readdir($dh)) {
$path = $dir_path.'/'.$el;
if (is_file($path)) {
// execute callback function with the file path ($el) as a parameter
call_user_func($func, $el);
}
}
closedir($dh);
} else {
return false;
}
return true;
}
// example usage
$dir_path = 'imgs/';
$web_path = 'imgs/';
echo '<div class="gallery">';
read_dir_elements($dir_path, 'make_gallery_link');
echo '<div class="sep"></div>';
echo '</div>';
echo 'some more content';
// write the gallery link given the filename
function make_gallery_link($filename) {
global $web_path;
$img_name = preg_replace("/\.([^\.]*)$/", '', $filename);
echo '<div class="img">';
echo '<img src="'.$web_path.$filename.'" border="0" />';
echo '<span class="img_label"><a href="'.$web_path.$filename.'">'.$img_name.'</a></span>';
echo '</div>';
}
?>
</body>
</html>
[/PHP]
Explanation:
The function below reads through all the files in a directory:
[PHP]/**
* Read files in a directory
* @author digital-ether at fijiwebdesign.com
*
* @param string directory path
*/
function read_dir_elements($dir_path, $func) {
if ($dh = opendir($dir_path)) {
while ($el = readdir($dh)) {
$path = $dir_path.'/'.$el;
if (is_file($path)) {
// execute callback function with the file path ($el) as a parameter
call_user_func($func, $el);
}
}
closedir($dh);
} else {
return false;
}
return true;
}[/PHP]
The bit of code:
[PHP]if (is_file($path)) {
// execute callback function with the file path ($el) as a parameter
call_user_func($func, $el);
}[/PHP]
checks if the element just read is a file. If it is, it then calls a callback function that you defined in read_dir_elements($dir_path, $func) as the second parameter.
I've created the function "make_gallery_link()" so that it can be called each time a file is read in the directory:
[PHP]// write the gallery link given the filename
function make_gallery_link($filename) {
global $web_path;
$img_name = preg_replace("/\.([^\.]*)$/", '', $filename);
echo '<div class="img">';
echo '<img src="'.$web_path.$filename.'" border="0" />';
echo '<span class="img_label"><a href="'.$web_path.$filename.'">'.$img_name.'</a></span>';
echo '</div>';
}[/PHP]
This function outputs an image in the directory in the format:
[HTML]
<div class="img">
<img src="imgs/icon_exclaim.gif" border="0" />
<span class="img_label">
<a href="imgs/icon_exclaim.gif">icon_exclaim</a>
</span>
</div>[/HTML]
Note: I've just done this so that it is easy to style with CSS. It makes your php coding simpler also as you do not have to worry about the structure of the html output.
The main this is just that readdir($dh) returns a string, which is the name of the file or subdirectory in the directory being read. This string is then used to create the url and src to the img file.
Hope that helps.
What you want to do is:
Open a directory handle and read each element in the directory.
When you read a directory, it returns all the elements in the directory.
Some of these are files, and some folders and it will also return two "virtual" folders "." and "..".
When you read a directory element usign readdir(/* opendir handle */) it will return the element name, such as the file name, or the directory name.
What you need is to get the file name, and create a link with it.
Example:
Here's an example page that should do what you want. In the example all files in the subdirectory "imgs" will be read and displayed by the php script.
[PHP]
/<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Show Images in Folder (simple gallery)</title>
<style type="text/css">
.img {
border: 1px solid #ccc;
float: left;
text-align: center;
padding: 2px;
}
.img_label {
border-bottom: 1px dotted #eee;
display: block;
}
.sep {
clear: both;
}
</style>
</head>
<body>
<?php
/**
* Read files in a directory
* @author digital-ether at fijiwebdesign.com
*
* @param string directory path
*/
function read_dir_elements($dir_path, $func) {
if ($dh = opendir($dir_path)) {
while ($el = readdir($dh)) {
$path = $dir_path.'/'.$el;
if (is_file($path)) {
// execute callback function with the file path ($el) as a parameter
call_user_func($func, $el);
}
}
closedir($dh);
} else {
return false;
}
return true;
}
// example usage
$dir_path = 'imgs/';
$web_path = 'imgs/';
echo '<div class="gallery">';
read_dir_elements($dir_path, 'make_gallery_link');
echo '<div class="sep"></div>';
echo '</div>';
echo 'some more content';
// write the gallery link given the filename
function make_gallery_link($filename) {
global $web_path;
$img_name = preg_replace("/\.([^\.]*)$/", '', $filename);
echo '<div class="img">';
echo '<img src="'.$web_path.$filename.'" border="0" />';
echo '<span class="img_label"><a href="'.$web_path.$filename.'">'.$img_name.'</a></span>';
echo '</div>';
}
?>
</body>
</html>
[/PHP]
Explanation:
The function below reads through all the files in a directory:
[PHP]/**
* Read files in a directory
* @author digital-ether at fijiwebdesign.com
*
* @param string directory path
*/
function read_dir_elements($dir_path, $func) {
if ($dh = opendir($dir_path)) {
while ($el = readdir($dh)) {
$path = $dir_path.'/'.$el;
if (is_file($path)) {
// execute callback function with the file path ($el) as a parameter
call_user_func($func, $el);
}
}
closedir($dh);
} else {
return false;
}
return true;
}[/PHP]
The bit of code:
[PHP]if (is_file($path)) {
// execute callback function with the file path ($el) as a parameter
call_user_func($func, $el);
}[/PHP]
checks if the element just read is a file. If it is, it then calls a callback function that you defined in read_dir_elements($dir_path, $func) as the second parameter.
I've created the function "make_gallery_link()" so that it can be called each time a file is read in the directory:
[PHP]// write the gallery link given the filename
function make_gallery_link($filename) {
global $web_path;
$img_name = preg_replace("/\.([^\.]*)$/", '', $filename);
echo '<div class="img">';
echo '<img src="'.$web_path.$filename.'" border="0" />';
echo '<span class="img_label"><a href="'.$web_path.$filename.'">'.$img_name.'</a></span>';
echo '</div>';
}[/PHP]
This function outputs an image in the directory in the format:
[HTML]
<div class="img">
<img src="imgs/icon_exclaim.gif" border="0" />
<span class="img_label">
<a href="imgs/icon_exclaim.gif">icon_exclaim</a>
</span>
</div>[/HTML]
Note: I've just done this so that it is easy to style with CSS. It makes your php coding simpler also as you do not have to worry about the structure of the html output.
The main this is just that readdir($dh) returns a string, which is the name of the file or subdirectory in the directory being read. This string is then used to create the url and src to the img file.
Hope that helps.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
![]() |
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Newbie question (C)
- Router Security Question... (Networking Hardware Configuration)
- Redirecting - Newbie Question (PHP)
- Link source code and header file together? (C++)
- AGP Aperture Size Question (Windows NT / 2000 / XP / 2003)
- How to network two Win98 machines (Networking Hardware Configuration)
Other Threads in the PHP Forum
- Previous Thread: connect one server from another server through script
- Next Thread: PHP installation possible errors?


Linear Mode