Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP Gallery

Status
Not open for further replies.

TheSponge

Technical User
Jul 2, 2003
442
GB
I have a simple upload script for my site,

all pictures goto
is there a simple way to make a gallery that would show these pics?

or a random picture of the day?

I have searched everywhere, but the php gallerys seem to be pre-made packages

Thanks

A+,Network +,CCNA
 
You can use functions such as [blue]opendir()[/blue], to get the contents of your upload folder and then show the images.
Look it up in the php online manual.

Its a very simple process. And can yield very interesting results. I had to do it once, and it took me one afternoon, let me see if i can find my code to show you, and i'll post back.





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
you could also have your upload script modify a text or csv file with the filename of the image and a title or something simmilar.

Then you would just read in the text file into php and extract the image filenames and print out the <img> tags.

Robert Carpenter
Remember....eternity is much longer than this ~80 years we will spend roaming this earth.
ô¿ô
 
OK thanks guys, Im fairly new to PHP, so any code you might have that could guide me is appreciated,

thanks

A+,Network +,CCNA
 
Ok I got this code which works...but Its need tidying up..

<?php
$dir = "march/";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
echo "filename: $file : filetype: " . filetype($dir . $file) . "\n";
}
closedir($dh);
}
}
?>

I also ammended a line to actually show the pics:

echo '<img src="'.$dir.''.$file.'">';

It shows the images, apart from 2 red x`s as the first two..(no idea why, all the directory images are showing..

and I would like them in a table, say 3 per row?

thanks so much so far...



A+,Network +,CCNA
 
Because the first 2 are not images but the directory pointers "." and ".." so it can't get an image from those. you need something like:[red]if(($file!=".")&&($file!=".."))[/red] to avoid getting those.

With that in mind put the image path's into an array, so you can then manipulate them such as:

Code:
if (is_dir($dir)) {
[green]$file_count=0;[/green]
   if ($dh = opendir($dir)) {
       while (($file = readdir($dh)) !== false) {
[red]if(($file!=".")&&($file!="..")){           [/red]
[green]$files[$file_count]=$dir.$file;
$file_count++;[/green]
[red]}[/red]
       }
       closedir($dh);
   }
}
Then just output 3 at a time:
echo "<table border=2><tr>";
$row=1;
for($j=0;$j<=$file_count-1;$j++){
if($row==4){echo "</tr><tr>"; $row=1;}
echo "<td><img src=" . $files[$j ."]></td>";
$row++;
}
echo "</tr></table>";

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
awesome! I understand , thankyou so much...

I can work on this,

much appreciated...

A+,Network +,CCNA
 
ok get an error now? Parse error: parse error, unexpected T_STRING



<?php
$dir = "uploads/";

// Open a known directory, and proceed to read its contents


if (is_dir($dir)) {
$file_count=0;
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if(($file!=".")&&($file!="..")){
$files[$file_count]=$dir.$file;
$file_count++;
}
}
closedir($dh);
}
}
Then just output 3 at a time:
echo '<table border=2><tr>';
$row=1;
for($j=0;$j<=$file_count-1;$j++){
if($row==4){echo "</tr><tr>"; $row=1;}
echo '<td><img src=" . $files[$j ."]></td>';
$row++;
}
echo '</tr></table>';

?>


thanks

A+,Network +,CCNA
 
you don't say on which line you get the error. but this line looks a bit odd:
Code:
echo '<td><img src=" . $files[$j ."]></td>';

and this line should be commented out (you should post your code between proper TGML tags to make readability easier
Code:
Then just output 3 at a time: //NEEED TO COMMENT THIS LINE
 
Yes, you need to move the bracket outside of the quotes. so it closes the $files array.


Then quote whatever is text. I just wrote that off the top if my head, so you need to check it before you try to run it.



----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
And this

Code:
echo '<td><img src=" . $files[$j ."]></td>';

should be

Code:
echo '<td><img src="[b][red]'[/red][/b] . $files[$j[b][red]][/red][/b] . [b][red]'[/red][/b]"></td>';

Lee
 
ill try these things thankyou..

A+,Network +,CCNA
 
Seems to work fine, just need khow how to shrink down the images so its all a bit neater,


Thanks everyone

A+,Network +,CCNA
 
Add specific size attributes to the [red]<img>[/red] tags like this:

Code:
echo "<td><img [blue]width='[red]xxx[/red]' height='[red]yyy[/red]'[/blue] src=" . $files[$j ."]></td>";

To whatever size you want them to be.

Carefull though as the images might, and most probably will look distorted when you apply the specific dimension attributes. To make them the size you want. However once the click on an image they will be shown in there original size and with no distortion.





----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
to add to vacunita's post: remember also that reducing the physical size at which an image displays does not reduce the logical size of an image. for this you will need to edit by hand and/or use imagemagik or gd libraries.
 
Yeah, as the images range from small gifs to large Jpegs, it looks a bit silly ;)

Thankyou , Ill work on it...

A+,Network +,CCNA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top