Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
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

macrumors 601
Jul 18, 2002
4,402
11
toronto
maybe i'm misunderstanding you, but, yes, you just use your php code to echo html. for example:
PHP:
echo "<font size=\"2\" color=\"black\">";
echo "<p align=\"center\">";
echo $data;
echo "</p></font>";
 

jtalerico

macrumors 6502
Original poster
Nov 23, 2005
358
0
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....

PHP:
#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

macrumors 601
Jul 18, 2002
4,402
11
toronto
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?
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
jtalerico said:
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

macrumors 6502
Original poster
Nov 23, 2005
358
0
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

macrumors G3
May 28, 2005
9,234
3,483
Pennsylvania
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

macrumors G3
May 28, 2005
9,234
3,483
Pennsylvania
PHP:
<?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 .'&return=gallery&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&page='. $view_minus .'">⇐⇐</a> | <a href="gallery.php">Home</a> | <a href="?x=gallery&page='. $view_plus .'">⇒⇒</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 .'&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

macrumors G3
May 28, 2005
9,234
3,483
Pennsylvania
jtalerico said:
//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

macrumors 6502
Original poster
Nov 23, 2005
358
0
No need for admin panel or that jazz....
I am guessing there is no way to do what i want with my code?
 

zimv20

macrumors 601
Jul 18, 2002
4,402
11
toronto
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

macrumors 6502
Original poster
Nov 23, 2005
358
0
zimv20 said:
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

macrumors 601
Jul 18, 2002
4,402
11
toronto
assuming that's right, it's similar to what i did on one of my sites, starting here.

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

macrumors 601
Jul 18, 2002
4,402
11
toronto
jtalerico said:
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

macrumors 6502
Original poster
Nov 23, 2005
358
0
zimv20 said:
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

macrumors 601
Jul 18, 2002
4,402
11
toronto
jtalerico said:
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

macrumors 6502
Original poster
Nov 23, 2005
358
0
zimv20 said:
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

macrumors 601
Jul 18, 2002
4,402
11
toronto
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:
<!-- 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

macrumors 601
Jul 18, 2002
4,402
11
toronto
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

macrumors 6502
Original poster
Nov 23, 2005
358
0
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

macrumors 601
Jul 18, 2002
4,402
11
toronto
jtalerico said:
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:

PHP:
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

macrumors 6502
Original poster
Nov 23, 2005
358
0
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.