944,061 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 4720
  • PHP RSS
Nov 2nd, 2006
0

Newbie Question - How to generate a link

Expand Post »
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:

PHP Syntax (Toggle Plain Text)
  1. <a href="./images/gallery/photo1.php"><img src="./images/gallery/thumbs/tn_photo1.php"></a>
  2. <a href="./images/gallery/photo2.php"><img src="./images/gallery/thumbs/tn_photo2.php"></a>
  3. <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
Similar Threads
Reputation Points: 14
Solved Threads: 0
Light Poster
okparrothead is offline Offline
31 posts
since Sep 2006
Nov 3rd, 2006
1

Re: Newbie Question - How to generate a link

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.

Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 3rd, 2006
0

Re: Newbie Question - How to generate a link

digital-ether,

I've tried to create a script like this on more than one occasion, but no joy. Your instructions have finally gotten through my clumsy mind.

Thank you SO much.

Peace
Reputation Points: 14
Solved Threads: 0
Light Poster
okparrothead is offline Offline
31 posts
since Sep 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: connect one server from another server through script
Next Thread in PHP Forum Timeline: PHP installation possible errors?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC