PDA

View Full Version : PHP Question




jtalerico
Feb 20, 2006, 05:15 PM
So I have a two loops, one to open a directory, then check to see the files in a directory. If one of those files happens to be a direcotry then it opens another loop and goes though those files... So basically all the files in these directories are images. So when the loops go through them it will push them into a string (all the image location in html format). Then it will fwrite() the string to a file. My question is, is there a way to create a temp page that will display those images, instead of fwriting() to a file? Maybe some method ? Any help would be great!



zimv20
Feb 20, 2006, 05:30 PM
maybe i'm misunderstanding you, but, yes, you just use your php code to echo html. for example:

echo "<font size=\"2\" color=\"black\">";
echo "<p align=\"center\">";
echo $data;
echo "</p></font>";

jtalerico
Feb 20, 2006, 05:40 PM
yeah, i know that much.. Problem is is that there is more than one dir. So for each Dir what i want is..

1 Main image for every directory that once clicked on will open the temp page that will have the rest of the files on it.

Here is my code so far....

#Opens Main Dir
$hdl = opendir('images/photos/');
#Grabs all the files of it except ones that being with '.'
while ($dirEntry = readdir($hdl)){
if (substr($dirEntry, 0,1) != '.') {
$listing[] = $dirEntry;
}
}
#Sort the entries
natsort($listing);
#loop through the files
foreach($listing as $file){
#if one of the files is a dir then open it
#and do the same thing which is listed above.. This will only
#go to the second level of dirs.
if(is_dir("images/photos/".$file)){
$hdl2 = opendir("images/photos/".$file);
while ($dirEntry2 = readdir($hdl2)){
if (substr($dirEntry, 0, 1) != '.') {
$listing2[] = $dirEntry2;
}
}
natsort($listing2);

#Close the files
closedir($hdl);
closedir($hdl2);

//Insead of doing the following, I want to have a temp page that is php
//generated. I want the page to be only "alive" as long as the user
//has the window open. Once it is closed, there is no way to get back to
//it without re-clicking on the link.


#File to be written to...
$ranfile = "/Users/joetalerico/Sites/txt/gallery/picture.txt";
#Open
$handle = fopen($ranfile, "rb+");
#Go back to the beginning
rewind($handle);

#First image of the first Dir and location... picture.php is only for temp use.
echo "<a href=txt/gallery/picture.php>";
echo "<img src=images/photos/".$file."/".$listing2[2]." width=150 height=100>";
echo "</a>";

$string;
foreach($listing2 as $file2){
#Grabbin all the files as long as they are not dirs.
if(!is_dir("images/photos/".$file."/".$file2)){
$link = "<a href=../../images/photos/".$file."/".$file2." target=none>";
$image = "<img src=images../../images/photos/".$file."/".$file2. " width=150 height=100>";
$blink = "</a>";
$string = $string . $link . $image . $blink;
}
}
#writing/closing
fwrite($handle, $string);
fclose($handle);
}
#Files that are not in dirs
echo $file;
echo " <a href=images/photos/".$file." target=none>";
echo " <img src=images/photos/".$file." width=150 height=100>";
echo "</a>";
echo " ";
}

zimv20
Feb 20, 2006, 06:15 PM
so you're making some kind of file browser? and you don't want to traverse the directory until the user's clicked on it?

can you start at the top of what your project is?

jtalerico
Feb 20, 2006, 06:38 PM
zim

it is a "simple" gallery...

http://69.68.181.132/gallery.php

zimv20
Feb 20, 2006, 06:43 PM
http://69.68.181.132/gallery.php
i'm getting this error:

Not Found

The requested URL /gallery.php was not found on this server.

zimv20
Feb 20, 2006, 06:47 PM
it is a "simple" gallery...

perhaps the page demonstrates what you mean, but since i can't see it...

describe what you want in user terms. "a user sees this", "a user clicks on that and this happens".

i know that you asked a rather simple, direct question about php capabilities, but i find that programmers often design themselves into a corner and ask that seemingly simple, direct question which, when explored, leads to a better solution overall. that's why i'm asking.

jtalerico
Feb 20, 2006, 06:49 PM
Crap..

http://69.68.181.132/~joetalerico/gallery.php

jtalerico
Feb 20, 2006, 06:50 PM
The user wont see what is going on...

I think it is called Dynamic pages?

I want PHP to create a temp site that will only be used once (the one time the user clicks on the link).


//Insead of doing the following, I want to have a temp page that is php
//generated. I want the page to be only "alive" as long as the user
//has the window open. Once it is closed, there is no way to get back to
//it without re-clicking on the link.


#File to be written to...
$ranfile = "/Users/joetalerico/Sites/txt/gallery/picture.txt";
#Open
$handle = fopen($ranfile, "rb+");
#Go back to the beginning
rewind($handle);

thejadedmonkey
Feb 20, 2006, 06:57 PM
Now I know that this isn't what you asked for, but it's a self-contained PHP script for a gallery. Just save it as gallery.php and put the images into a "gallery" folder, After you go through it, maybe you'll be inspired

P.S. It's below, since it was too long for this post. Also, it's missing some comments to get it to fit..PM me if anyone wants the full script unedited.

thejadedmonkey
Feb 20, 2006, 06:59 PM
<?php
////////////////////////////////////////////
///////////////CONFIG SETTINGS//////////////
////////////////////////////////////////////
////////////http://www.rilet.com////////////
////////////////////////////////////////////

// Title
$title = "photos";

// Administrator Password
$password = "password";

// Set a number of pictures to display per
// page while viewing in gallery view
$pictures = "12";

// Display "View All" on main menu.
// This setting is best set to "off" if
// you are concerned about server load.
$view_all = "on";

// Maximum picture size before it is
// reduced while viewing photo as a
// full size image
$picture_width = "800";
$picture_height = "600";

// Folder for storing photos (w/o trailing slash)
$image_folder = "gallery";

// Display method for gallery view-
// "height", "width" or "none".
$viewtype="height";

// Set a maximum height and width for
// pictures displayed in gallery
$width = 100;
$height = 100;


//////////END OF CONFIGURATION SETTINGS::: DO NOT EDIT BELOW THIS LINE!!!
@$x = $_GET['x'];

if ($x != "resize"){
echo'
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
<html>
<head><meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>'. $title .' | Photo Gallery v 1.2.2</title>
<style>

a {color:white;}
a:hover {color:#003366;}

body {
background:#0099CC;
color:#DDDDDD;
font:12pt Comic Sans MS;
}

img {border:1px solid white;}
.gallery:hover {border:1px dotted #888888;}

#title{
text-align:center;
}

#gallery{
width:600px;
margin:auto;
}
#nav{text-align:center;}
#image {text-align:center;}

</style>
</head>
<body>
';
}

// Resize for gallery
//////////////////////////////////////////////////////////////////////////////////
if ($x == "resize"){

// The file
$filename = $_GET['filename'];
$filename = "$image_folder".'/'."$filename";

header('Content-type: image/jpeg');
list($width_orig, $height_orig) = getimagesize($filename);

if ($viewtype == "height"){
$width = ($height / $height_orig) * $width_orig;
}else if ($viewtype == "width"){
$height = ($width / $width_orig) * $height_orig;
}else if ($viewtype == "none"){
if ($width_orig < $height_orig) {
$width = ($height / $height_orig) * $width_orig;
} else {

$height = ($width / $width_orig) * $height_orig;
}
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
imagejpeg($image_p, null, 100);

imagedestroy($image_p);

// Gallery view-by-page
}else if ($x == "gallery"){

@$view = $_GET['page'];
if (empty($view)){
$view = 0;
}
$view_plus = $view+$pictures;
$view_minus = $view-$pictures;

echo'<div id="title">'. $title .'</div><div id="gallery">';

$file = glob("$image_folder/*.*");
$file = array_slice($file, $view, $pictures);
//sort by date...

foreach ($file as $files){

$file = str_replace("$image_folder".'/', "", $files);
echo '<a href="gallery.php?x=photo&view='. $file .'&amp;return=gallery&amp;page='. $view .'" title="'. $file .'"><img src="gallery.php?x=resize&filename='. $file .'" class="gallery" /></a>'."\n";
}

echo'</div>
<div id="nav"><a href="?x=gallery&amp;page='. $view_minus .'">&lArr;&lArr;</a> | <a href="gallery.php">Home</a> | <a href="?x=gallery&amp;page='. $view_plus .'">&rArr;&rArr;</a></div>';

// Individual Photo
}else if ($x == "photo"){

@$view = $_GET['view'];
@$return = $_GET['return'];
@$page = $_GET['page'];

$size = getimagesize("$image_folder".'/'."$view");
$w = $size[0];
$h = $size[1];

echo'<div id="image">';

if ($w > $picture_width || $h > $picture_height){
echo'<img src="'. $image_folder .'/'. $view .'" height="90%" width="90%" />';
}else{
echo'<img src="'. $image_folder .'/'. $view .'" />';
}

//Display Comment.
echo"<br>";
include("gallery.csv");
echo @$caption["$view"];


echo'</div>
<div id="nav"><a href="gallery.php?x='. $return .'&amp;page='. $page .'">Back</a></div>';

// Gallery view-all
}else if ($x == "all"){
$handle = opendir("$image_folder");

echo'<div id="title">'. $title .'</div><div id="gallery">';

while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != ".DS_Store" && $file != "Thumbs.db") {


echo '<a href="gallery.php?x=photo&view='. $file .'&return=all" alt="'. $file .'" title="'. $file .'"><img src="gallery.php?x=resize&filename='. $file .'" class="gallery" /></a>'."\n";

}
}
echo'</div>
<div id="nav"><a href="gallery.php">Back</a></div>';
closedir($handle);

// Begin admin pages
}else if ($x == "admin"){

@$y = $_GET['y'];

if (isset($y)){

// Check for password or error
$pass = $_POST['pass'];

if ($pass == $password){

// Main page

if ($y == "admin"){

echo'<p><big>Admin page</big><br />
<a href="gallery.php">Home</a></p>
<hr />
<form enctype="multipart/form-data" action="gallery.php?x=admin&y=upload" method="post">
<input name="imagefile" type="file" />
<input type="hidden" name="pass" value="'. $pass .'" />
<input type="submit" value="Upload" /><br>
Note <input type="text" name="caption" style="width:13em;" maxlength="150">
</form>
<hr>
';

$handle = opendir("$image_folder");
require("gallery.csv");

while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {

@$thecaption = $caption["$file"];

echo'<form action="gallery.php?x=admin&y=delete" method="post"><a href="'. $image_folder .''. $file .'" title="'. $file .'">View</a> | ';
echo'<a href="#" onCLick="alert(\'This is a spaceholder until a caption edit function is added.\')">Edit</a> | ';
echo'<input type="hidden" name="delete" value="'.$file.'"><input type="hidden" name="pass" value="'.$pass.'">
<input type="hidden" name="caption" value="'.$thecaption.'"><input type="submit" value="Delete" onCLick="return confirm(\'Are you SURE you want to delete this entry?\')"> ';
echo"$file<br /></form>\n";
}
}

closedir($handle);
echo'<hr>';

// Upload page
}else if ($y == "upload"){

if ($_FILES['imagefile']['type'] == "image/jpeg"){
$name = $_FILES['imagefile']['name'];


//Code to add captions to file.
@$caption = trim(stripslashes(strip_tags($_POST['caption'])));
$file = file_get_contents("gallery.csv");
$fp = fopen("gallery.csv", "w+");

if(!$fp){ //Check to make sure file is writable, halts the script if not.
print "The file \"gallery.csv\" could not be written to.";
exit;
}

// Limits the shout size entered by the user.
if (strlen($caption) >= 200) {
$caption = substr($caption, 0, 200);
$caption = $caption."...";
}
$caption = "\$caption['$name'] = \"$caption\";";
$caption = str_replace( '?>', $caption, $file );
fwrite($fp, "$caption\n?>");
fclose($fp);

//Code to upload image to server.
copy ($_FILES['imagefile']['tmp_name'], "$image_folder".$_FILES['imagefile']['name'])
or die ("Could not copy");



echo "";
echo "Name: $name<br>";
echo "Size: ".$_FILES['imagefile']['size']."<br>";
echo "Upload Done....";

echo '<form action="gallery.php?x=admin&y=admin" method="post"><input type="hidden" name="pass" value="'. $pass .'"><input type="submit" value="Continue"></form>';
}

else {
echo "<br><br>Could Not Copy, Wrong Filetype $name<br>";
}
// To edit a caption
//////////////////////////////////////////////////////////////////////////////
}else if ($y == "edit_caption"){

echo"This is a dummy page until I can get around to writing it.";

// To delete a file

}else if ($y == "delete"){
$delete = $_POST['delete'];
$caption = $_POST['caption'];
$handle = opendir("$image_folder");

unlink("$image_folder"."$delete")
or die("There was an error deleting the file!");
closedir($handle);


$file = file_get_contents("gallery.csv");
$fp = fopen("gallery.csv", "w+");

$caption = "\$caption['$delete'] = \"$caption\";";
echo"$caption";
$update = str_replace( "$caption", '', $file );
fwrite($fp, "$update");
fclose($fp);

echo'File deleted<br /><form action="gallery.php?x=admin&y=admin" method="post"><input type="hidden" name="pass" value="'. $pass .'"><input type="submit" value="Continue"></form>
';
}

}else{
echo "bad password";
}
// Login page
//////////////////////////////////////////////////////////////////////////////
}else{

echo'
<form action="gallery.php?x=admin&y=admin" method="post"><p>Login:<br />
<input name="pass" type="password" /><input value="Log in" type="submit" />
</p></form>
';

}



// Main Menu
//////////////////////////////////////////////////////////////////////////////////
}else{

echo'
'. $title .'
<ul>
<li><a href="gallery.php?x=gallery">View</a></li>';
if ($view_all == "on"){
echo'<li><a href="gallery.php?x=all" onCLick="return confirm(\'Warning: This page may take a while to load, and is not recomended for users with slower connections. Do you still want to view all?\')">View all</a></li>';
}
echo'<li><a href="gallery.php?x=admin">Administer</a></li>
</ul>
';
}


if ($x != "resize"){
echo"</body></html>";
}
?>

thejadedmonkey
Feb 20, 2006, 07:02 PM
//Insead of doing the following, I want to have a temp page that is php
//generated. I want the page to be only "alive" as long as the user
//has the window open. Once it is closed, there is no way to get back to
//it without re-clicking on the link.

Why not make each link a form submit, and have it contain a hidden password via post. That way, even if you do access the link from some other source, it doesn't have the post password, and fails.

Or, why not have a page that is deleted after an hour or so...I know you'd want to use croan tasks for this, but also, if it's frequently accessed, it won't help.

P.S. Sorry for the tripple posts.

jtalerico
Feb 20, 2006, 07:24 PM
No need for admin panel or that jazz....
I am guessing there is no way to do what i want with my code?

zimv20
Feb 20, 2006, 07:27 PM
joe -- if your example page is close to what you want, then i think you can do it w/o having to do any file writes.

lemme see if i understand the user experience:

1. the user is presented with a screen of image thumbnails and directory icons
2. if the user clicks an image thumbnail, a new window is opened with the full-size image
3. if the user clicks on a directory icon, the page reloads and presents itself as #1, but with new data

is that basically right?

jtalerico
Feb 20, 2006, 07:39 PM
joe -- if your example page is close to what you want, then i think you can do it w/o having to do any file writes.

lemme see if i understand the user experience:

1. the user is presented with a screen of image thumbnails and directory icons
2. if the user clicks an image thumbnail, a new window is opened with the full-size image
3. if the user clicks on a directory icon, the page reloads and presents itself as #1, but with new data

is that basically right?

1. User is presented with images that are linked to a directory.
2. Once the user clicks the image, it loads other thumbnails that are in the directory.

I am so sorry, i am trying to explain this.. It is pretty tough..

zimv20
Feb 20, 2006, 07:41 PM
assuming that's right, it's similar to what i did on one of my sites, starting here (http://www.boxsetauthentic.com/shovelchoir.php).

all that song data is being pulled from a MySQL database. if you click on one of the View links in the 'Lyrics and Info' column, you'll go to a page with a URL like this:

http://www.boxsetauthentic.com/showlyrics.php?song_id=148

that page data is being built dynamically, based on the song_id passed in. you'll see that if you go to the page showlyrics.php, you'll get an error.

is that basically what you're after?

zimv20
Feb 20, 2006, 07:43 PM
1. User is presented with images that are linked to a directory.
2. Once the user clicks the image, it loads other thumbnails that are in the directory.

that helps a lot. yeah, see what i've posted just before. i think that approach will work for you.

jtalerico
Feb 20, 2006, 07:49 PM
that helps a lot. yeah, see what i've posted just before. i think that approach will work for you.

That is basically what i am after.. Can it only be done with a MySQL Database? If you have AIM I could do it talk to you there so you dont have to keep refreshing the page! :). But yes that is what I am after.. For PHP to build the temp page for me!

zimv20
Feb 20, 2006, 07:58 PM
That is basically what i am after.. Can it only be done with a MySQL Database? If you have AIM I could do it talk to you there so you dont have to keep refreshing the page! :). But yes that is what I am after.. For PHP to build the temp page for me!
no chat, sorry.

php works w/ other databases, but aren't you working with the filesystem? for your argument, where i use song_id, you could actually use the directory path. or is the hierarchy defined in a db?

jtalerico
Feb 20, 2006, 07:59 PM
no chat, sorry.

php works w/ other databases, but aren't you working with the filesystem? for your argument, where i use song_id, you could actually use the directory path. or is the hierarchy defined in a db?

Right i would use the _id as the dir name.. but how does that create the php/html page?

zimv20
Feb 20, 2006, 08:05 PM
this is the code for showlyrics.php. go to the bottom, for the "main" routine, and you'll see how i first use the passed in variable.


<!-- php code and design copyright steve zimmers, 2004 -->

<?
include "header.inc";
include "style.css";
?>

<body>

<?php
include "songcode.inc";

//-----------------------------------------------------------
// displaySongTitle
//-----------------------------------------------------------

function displaySongTitle($song_name, $version)
{
?>
<h3 class="songtitle">
<?php
echo $song_name;
newline();

if ($version != null)
echo $version;
?>
</h3>
<?php
}

//-----------------------------------------------------------
// displayNotes
//-----------------------------------------------------------

function displayNotes($notes)
{
?>
<p class="block">
<?php
echo $notes;
newline();
?>
</p>
<?php
}

//-----------------------------------------------------------
// displaySongLyrics
//-----------------------------------------------------------

function displaySongLyrics($lyrics)
{
?>
<p class="lyrics">
<?php
if ($lyrics != null)
echo $lyrics;
else
echo "No lyrics submitted";
?>
</p>
<?php
}

//-----------------------------------------------------------
// displaySongAccr
//-----------------------------------------------------------

function displaySongAccr($accr, $special)
{
?>
<p class="attributation">
<?php
foreach ($accr as $accr_name => $artists)
{
echo $accr_name . ": " . $artists;
newline();
}

if ($special != null)
{
newline();
echo $special;
}
?>
</p>
<?php
}

//-----------------------------------------------------------
// grabAndDisplaySongNotesAndLyrics
//-----------------------------------------------------------

function grabAndDisplaySongNotesAndLyrics($song_id, $conn)
{
$row_query = 'SELECT a.song_name, a.version, a.lyrics, a.notes'
. ' FROM songs a'
. ' WHERE a.song_id = '. $song_id;

if (!($row_result = @ mysql_query($row_query, $conn)))
showerror();

while ($row = mysql_fetch_array($row_result))
{
$song_name = $row["song_name"];
$version = $row["version"];
$lyrics = $row["lyrics"];
$notes = $row["notes"];

displaySongTitle($song_name, $version);

if ($notes != null)
displayNotes($notes);

if ($lyrics != null)
displaySongLyrics($lyrics);
}
}

//-----------------------------------------------------------
// grabAndDisplaySongAccr
//-----------------------------------------------------------

function grabAndDisplaySongAccr($song_id, $conn)
{
$row_query = 'SELECT b.accr_name, d.artist_name, a.special_accreditation'
. ' FROM songs a, accreditation b, song_accr_assign c,'
. ' artists d'
. ' WHERE a.song_id = '. $song_id
. ' AND a.song_id = c.song_id'
. ' AND c.artist_id = d.artist_id'
. ' AND c.accr_id = b.accr_id';

if (!($row_result = @ mysql_query($row_query, $conn)))
showerror();

$found_data = 0;

while ($row = mysql_fetch_array($row_result))
{
$found_data = 1;

$artist_name = $row["artist_name"];
$accr_name = $row["accr_name"];
$special = $row["special_accreditation"];

if ($accr[$accr_name] == null)
$accr[$accr_name] = $artist_name;
else
$accr[$accr_name] .= ', ' . $artist_name;
}

if ($found_data)
displaySongAccr($accr, $special);
}

//-----------------------------------------------------------
// main
//-----------------------------------------------------------

$user = "xxx";
$pword = "xxx";
$db = "xxx";

$conn = mysql_connect("localhost", $user, $pword)
or showerror();

mysql_select_db($db);

// song_id is passed in as URL argument

grabAndDisplaySongNotesAndLyrics($song_id, $conn);
grabAndDisplaySongAccr($song_id, $conn);

if (!mysql_close($conn))
showerror();
?>

</body>

<?
include "footer.inc";
?>

zimv20
Feb 20, 2006, 08:09 PM
btw, though i've been programming for many years, php is pretty new to me. this code and the rest of the site was my learning project. but it all seems to work pretty well.

so -- for all you studly, experienced php programmers, if when looking at my code you see me doing odd things or there are easier ways to accomplish what i'm trying to do, i'd love some notes.

jtalerico
Feb 20, 2006, 08:10 PM
Hmmmm... I might have to hold off on this Dynamic stuff.. First off my Mysql is not running very well on my sever.. (I think i have to delete it and install it again)... And I am not that great with MySQL.. I was kind of hoping to just keep my current code..


unless i can do something like make a function in my current php file that is like your main that i will...
get_id()
then find the the dir name that is assoicated with the get_id()... Could that work?

zimv20
Feb 20, 2006, 08:17 PM
Hmmmm... I might have to hold off on this Dynamic stuff.. First off my Mysql is not running very well on my sever..
maybe i'm not understanding, but i don't see where you need it. the filesystem you're representing effectively works as the database, if you know what i mean.

when you have a URL of the form:

foo.com/showdir.php?display_dir="images/bathroom/toilet"

... in showdir.php, you can simply access your variable display_dir:


echo $display_dir;


you don't have to do anything special to get it. i didn't mean to throw you with all the database code, but i did want to give you the whole file, so that you could match up my rendered page to the code you saw.

jtalerico
Feb 20, 2006, 08:25 PM
I would like to have my little PHP page with it.. Have the tables and stuff like that with it.. Maybe i am not following what needs to be done! Gah!

zimv20
Feb 20, 2006, 08:38 PM
no! don't panic!

look at your php code, above. you've got "images/photos" hardcoded in there. start by making its value the variable you pass in from the URL.

then simplify your code to display only what's in that directory. for each image you're putting to the screen, make sure its URL conforms to what you're coding to. i.e. the page will have links to itself, but with different values for the variable.

so instead of:
http://69.68.181.132/~joetalerico/gallery.php

you'd have something like:
http://69.68.181.132/~joetalerico/gallery.php?display_dir="images/photos/house"

...and then, when the user clicked on the directory for kitchen...

http://69.68.181.132/~joetalerico/gallery.php?display_dir="images/photos/house/kitchen"

does that make sense?

zimv20
Feb 21, 2006, 03:33 AM
just for fun, i mocked up a little prototype here (http://www.zimfest.net/silly/). the start page is hardcoded. the images will take you to a subdirectory and run showdir.php with the URL of the subdirectory as an argument.

showdir.php will parse the directory and perform the following:
1. if it finds a file, it will assume it's an image and display it
2. if it finds a subdirectory, it will make a link to the subdirectory

so, if you click on the link, it calls showdir.php again, but with the new value. the best example of this is the bricks, you can follow that down a couple directories.

it's not the best code, or the best way to do it, but it's a working prototype. here's the php code:

showdir.php:

<?
//----------------------------------------------------------------
//
// %Z%%M% %I% %G%
//
// copyright Steve Zimmers 2006
//
//----------------------------------------------------------------

include "header.inc";
include "style.css";
?>

<body>

<?
//------------------------------------------------------------
// display_image
//------------------------------------------------------------

function display_image($file, $dir)
{
$pathed_file = $dir . "/" . $file;

$img_string = '<img src="' . $pathed_file . "\" " . 'width="200" height="150" />';

echo $img_string . '<br />' . '<br />' . "\n";
}

//------------------------------------------------------------
// make_url
//------------------------------------------------------------

function make_url($file, $dir)
{
$pathed_file = $dir . "/" . $file;

$href_string = '<a href="showdir.php?display_dir=' . $pathed_file . "\"" .'>' . $file . '</a>';

echo $href_string . '<br />' . '<br />' . "\n";
}

//------------------------------------------------------------
// main
//------------------------------------------------------------

// display_dir is our passed in variable. first we change to
// that directory in order to grab the file contents

chdir($display_dir);

// next we create our directory object in order to get
// the file list

$dir = dir('.') or die($php_errormsg);

// process each file

while (false !== ($f = $dir->read()))
{
// skip this directory and its parent

if ($f == "." || $f == "..")
continue;

// if it's a file, assume it's an image to display

if (is_file($f))
{
display_image($f, $display_dir);
continue;
}

// if it's a directory, make an href

if (is_dir($f))
{
make_url($f, $display_dir);
continue;
}


} // while

// clean up

$dir->close();

?>

</body>

<?
include "footer.inc";
?>


index.php:

<?
include "header.inc";
include "style.css";
?>

<body>

<a href="showdir.php?display_dir=images/chairs">
<img src=images/chairs.jpg width="200" height="150" />
</a>

<br />
<br />

<a href="showdir.php?display_dir=images/bricks">
<img src=images/bricks.jpg width="200" height="150" />
</a>

<br />
<br />

<a href="showdir.php?display_dir=images/trees">
<img src=images/trees.jpg width="200" height="150" />
</a>

</body>

<?
include "footer.inc";
?>


header.inc:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title>silly</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>


footer.inc:

</html>


style.css is empty.

jtalerico
Feb 21, 2006, 05:14 PM
Why does php think my two arrays are not arrays?

Warning: Invalid argument supplied for foreach() in /Users/joetalerico/Sites/gallery.php on line 23

Warning: natsort() [function.natsort]: The argument should be an array in /Users/joetalerico/Sites/gallery.php on line 32

Warning: natsort() [function.natsort]: The argument should be an array in /Users/joetalerico/Sites/gallery.php on line 33
<?
include ("global.inc");
top();
topmain();
bannertbl();
endtbl();
maintbl();
menu();
frstbl();
#Set directories...
$opendir = opendir('images/photos');
#Get the Files
while($files = readDir($opendir)){
#Dont want .something files
if(is_dir($file) == true){
if(substr($files, 0,1) != "."){
#put them in an array
$directory[] = $files;
}
}
}
#Go through the array of dir names
foreach($directory as $file){
#checks to see if it is a dir
$imgdir = opendir('images/photos/$file');
while($images = readDir($imgdir)){
if(substr($images, 0,1) != "."){
$files[] = $images;
}
}
}
natsort($files);
natsort($directory);
$num = 0;
echo $holddir[1];
while($dirs[num] != null && $img[num] != null){
$dirname = $dirs[num];
$image = $img[num];
echo '<a href=images/photos/?name='.$dirname.'>';
echo '<img src=images/photos/'.$dirname.' width=150 height=150>';
echo '</a>';
$num++;
}
endtbl();
endhtml();
?>

zimv20
Feb 21, 2006, 05:31 PM
couple things i noticed:

1) line 6: 'ture' should be 'true'
2) your lines don't match up with the compiler messages (need whole file or numbered lines)
3) because of 'ture', that array will never be filled; print them out to check their values as a debugging step

jtalerico
Feb 21, 2006, 05:36 PM
couple things i noticed:

1) line 6: 'ture' should be 'true'
2) your lines don't match up with the compiler messages (need whole file or numbered lines)
3) because of 'ture', that array will never be filled; print them out to check their values as a debugging step

Sorry about that, fixed the true still doesnt work.. There is the whole code

jtalerico
Feb 21, 2006, 05:43 PM
I found another problem.. File and files..


Also if i remove the if(substr($file, 0,1) part it does not error out...


Never mind!!
Got that working :)

jtalerico
Feb 21, 2006, 08:26 PM
So after learning some more php.. I have finally got this working... Pretty simple Gallery that is very upgradeable... (I am soon going to add comments for each gallery just a short blurb)..

So here it is... In all of its greatness.


gallery.php

<?
include ("global.inc"); #your global, or whatever you use to make it purrdy!
echo "<html>";
echo "<body>";
#Set directories...
$opendir = opendir("images/photos");
#Get the Files
$length = 0;
while($file = readdir($opendir)){
if((substr($file, 0,1)) != '.'){
#Dont want .something files
if(is_dir("images/photos/".$file) == true){
#put them in an array
$directory[] = $file;
$length++;
}
}
}
#Go through the array of dir names
foreach($directory as $dir){
#checks to see if it is a dir
$imgdir = opendir("images/photos/".$dir);
$fstimg = 0;
#Get the dir contents
while($images = readdir($imgdir)){
#No need to get more than one image per dir.
if($fstimg < 1){
if((substr($images, 0,1)) != "."){
$files[] = $images;
$fstimg++;
}
}
}
}
#No need to be open anymore!
closedir($imgdir);
closedir($opendir);
$num = 0;
#Get all the goods out of the array
while(sizeof($directory) > $num){
$dirname = $directory[$num];
$image = $files[$num];
#Make my front page!!
echo "<br>";
echo "<br>";
echo "<a href=gallery_img.php?dir_name=".$dirname.">";
echo "<img src=images/photos/".$dirname."/".$image. " width=200 height=150>";
echo '</a>';
#Update num so this doesn't go on for ever.. Trust me.. Firefox wont like it.
$num++;
}
echo "</body></html>";
?>


gallery_img.php

<?
include ("global.inc"); #your global, or whatever you use to make it purrdy!
echo "<html>";
echo "<body>";
#Grab everything after the ?
parse_str($_SERVER['QUERY_STRING'],$vars);
$dir_name = $vars['dir_name'];
#Opens Main Dir
$hdl = opendir("images/photos/".$dir_name);
#Grabs all the files of it except ones that being with '.'
while ($dirEntry = readdir($hdl)){
if (substr($dirEntry, 0,1) != '.') {
$listing[] = $dirEntry;
}
}
#Sort the entries, not that it matters...
natsort($listing);
#Close the files
closedir($hdl);
foreach($listing as $file){
#Grabbin all the files as long as they are not dirs.
if(!is_dir("images/photos/".$dir_name."/".$file)){
#Put it out to the site!
echo "<a href=images/photos/".$dir_name."/".$file." target=none>";
echo "<img src=images/photos/".$dir_name."/".$file. " width=150 height=100> ";
echo "</a>";
}
}
echo "</body></html>";
?>

Thats it... Hope it looks like decent code!