Jump to content

view uploaded pictures?


primefalcon

Recommended Posts

I've just finished putting together a file upload script...though even after looking through the w3schools, I cant seem to find out how to view all pictures files inside a directory folder, Is there a way to do this using php, or is something else needed here?

Link to comment
Share on other sites

Did you mean something like this?

<?php  $dir='images';  $scan=scandir($dir);  $allow=array('gif', 'jpg');  foreach ($scan as $file) {    $ext=str_replace('.', '', strrchr($file, '.'));    if (in_array($ext, $allow)) {      echo '<img src="'.$dir.'/'.$file.'">';    }  }?>

Link to comment
Share on other sites

Hi, this might be similar to something I am trying to do. I tried the code you provided mma_fighter, but when I tried it it said "Call to undefined function: scandir()" do you know what I could be doing wrong?
The scandir() is for those who use PHP 5, if you are running an older version you could use opendir(). I'll help you with it.
Link to comment
Share on other sites

Try this if you aren't using PHP 5

<?php  $dir='images';  $open=opendir($dir);  $allow=array('gif', 'jpg');  while (($file=readdir($open))!==false) {    $ext=str_replace('.', '', strrchr($file, '.'));    if (in_array($ext, $allow)) {      echo '<img src="'.$dir.'/'.$file.'">';    }  }  closedir($open);?>

Link to comment
Share on other sites

I've just been playing around the code a littleand heres is what I have so far:

<a href="index.php">Upload another file</a><p/><?php$dir='upload';$scan=scandir($dir);$allow=array('gif', 'png');foreach ($scan as $file) {$ext=str_replace('.', '', strrchr($file, '.'));if (in_array($ext, $allow)) {echo '<a target="_new" href="'.$dir.'/'.$file.'"><img width="20%" height="20%" src="'.$dir.'/'.$file.'"></a>    ';}}?>

so really all you would need to do is a table in there with a for each loop

Link to comment
Share on other sites

if (in_array($ext, $allow)) {echo '<a target="_new" href="'.$dir.'/'.$file.'"><img width="20%" height="20%" src="'.$dir.'/'.$file.'"></a>    ';}}?>[/code]
Can you explain this part a little to me? Is it creating a link to a larger image for each image?
Link to comment
Share on other sites

that is yes, mma or one of the others would be able to help with the table more though.but it would be accomplished with the while or foreach loop.though I'm not exactly sure how you would break into a new row after each 3 or so columns when your drawing a new fresh image each time

Link to comment
Share on other sites

and the % tags for size are basing the picture size on the percentage of the screen unless you have them in a frame or something, which it would then be based on that which is why they're all put at the same size at least in that example I gave

Link to comment
Share on other sites

primefalcon: html doesnt just wrap to the next line, I'm kind of confused here also as to why its breaking (if its actually happening). You need to add a line break (<br />) to the end of a line to make it wrap, or if you place it inside a container like a div or a table then it will break. Only then though.Looks like you have the listing open. By the way, what you want now is called pagination, just google it and I'm sure you can find some good scripts for it. (google php pagination)

Link to comment
Share on other sites

There's no secret to pagination, I'm not sure why people have a hard time with it. The only thing your script needs to know is which records to show on the page, right? So if you have 15 or 20 records per page, or whatever you want, then all your script needs to know is which page you're on now. You can pass a variable in the URL and retrieve the value using $_GET to tell you which page you are on now. Once you know that, you can figure out which records you need to get from the database. For example, if you're showing 10 per page and you're on page 3, you need records 21-30. You can use the LIMIT clause in SQL to get only those you need. This would get 10 records starting at number 21:

SELECT * FROM table LIMIT 21, 10

There were discussions about that in the forum recently, check the threads on the first couple pages.

Link to comment
Share on other sites

Justsomeguy, I think the problem with pagination is a little different in this thread than what you described because the images aren't using a MySQL database, they're just stored in a folder. Is there a way to limit results that are not in a database? I'm getting a little confused here because I'm trying to do this gallery two different ways. One is in another thread I started where I was putting the images into a database, but then I saw this thread that wasn't using a database and I decided to try this too.

Link to comment
Share on other sites

There's several ways to do that, the one I would recommend goes like this:

  1. Go thru the files (using scandir or opendir, or what you want).Instead of outputting the filenames (as I guess yo do now), save them in a array.
  2. Use an for-loop to loop thru the files (now you output the fileanames) (don't use foreach [you could, but that's overcomplicating things])

Here's how it could look like (excluding the "scandir-loop", including "default pagination values"):

$start = 0;$perPage = 15;if (isset($_GET['page']))   $start = $_GET['page'] * $perPage;for ($i = $start; ($i < ($start + $perPage)) && ($i < count( $files )); $i++) {   echo '<img src="' . $files[$i] . '" alt="" />'; // or what you want...}

It's not harder than that.. :?)Hope that help!Good Luck and Don't Panic!

Link to comment
Share on other sites

primefalcon: html doesnt just wrap to the next line, I'm kind of confused here also as to why its breaking (if its actually happening). You need to add a line break (<br />) to the end of a line to make it wrap, or if you place it inside a container like a div or a table then it will break. Only then though.Looks like you have the listing open. By the way, what you want now is called pagination, just google it and I'm sure you can find some good scripts for it. (google php pagination)
Once html does reach the end of a line it will break to the next page at the first possible space unless you do the   which means it cant break in that location. what <br/> does is force a line break in that position, which is all that's happening
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...