primefalcon Posted April 25, 2007 Share Posted April 25, 2007 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 More sharing options...
primefalcon Posted April 25, 2007 Author Share Posted April 25, 2007 Just a note too, I don't need a walk through, Just a pointer in the right direction so I know where to look.Thank you Link to comment Share on other sites More sharing options...
jesh Posted April 25, 2007 Share Posted April 25, 2007 I don't use PHP much, but check out opendir (http://us.php.net/function.opendir). You can use it to open up a directory and get all the files in the directory. Once you have all the files in an array, you can use that array to build HTML that can be used to display all the images. Link to comment Share on other sites More sharing options...
zppblood Posted April 25, 2007 Share Posted April 25, 2007 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 More sharing options...
primefalcon Posted April 25, 2007 Author Share Posted April 25, 2007 Thank you, thats is exactly as I was wanting, thank you so much Link to comment Share on other sites More sharing options...
music_lp90 Posted April 25, 2007 Share Posted April 25, 2007 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? Link to comment Share on other sites More sharing options...
zppblood Posted April 25, 2007 Share Posted April 25, 2007 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 More sharing options...
music_lp90 Posted April 25, 2007 Share Posted April 25, 2007 Thanks, now it says "Invalid argument supplied for foreach() " I think it might be the $file do I need to assign something to that? Link to comment Share on other sites More sharing options...
zppblood Posted April 25, 2007 Share Posted April 25, 2007 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 More sharing options...
music_lp90 Posted April 25, 2007 Share Posted April 25, 2007 Great, thanks. That works, if its not too much to ask, is there a way to insert these into an html table? Link to comment Share on other sites More sharing options...
primefalcon Posted April 25, 2007 Author Share Posted April 25, 2007 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 More sharing options...
music_lp90 Posted April 25, 2007 Share Posted April 25, 2007 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 More sharing options...
music_lp90 Posted April 26, 2007 Share Posted April 26, 2007 Ok, I just checked it out. Thats great, I don't understand how its adding new rows though, but it works well. If you can explain it to me I would appreciate it. Thanks for the help. Link to comment Share on other sites More sharing options...
primefalcon Posted April 26, 2007 Author Share Posted April 26, 2007 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 More sharing options...
music_lp90 Posted April 26, 2007 Share Posted April 26, 2007 Ok, thanks, but what you did is already breaking it into rows for me. So it's working pretty much exactly how I was hoping. I just don't understand why its breaking it into columns and rows without any <tr> or <td> tags. Link to comment Share on other sites More sharing options...
primefalcon Posted April 26, 2007 Author Share Posted April 26, 2007 well its reaching the end of the page is as with standard html, its just wrapping to the next line Link to comment Share on other sites More sharing options...
primefalcon Posted April 26, 2007 Author Share Posted April 26, 2007 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 More sharing options...
music_lp90 Posted April 26, 2007 Share Posted April 26, 2007 ok, yeah, I see now. Do you know if there is a way to limit the number of results per page and the display the rest on next pages? Link to comment Share on other sites More sharing options...
primefalcon Posted April 26, 2007 Author Share Posted April 26, 2007 yes, but thats something someone here more advanced will have to tell you Link to comment Share on other sites More sharing options...
reportingsjr Posted April 26, 2007 Share Posted April 26, 2007 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 More sharing options...
justsomeguy Posted April 26, 2007 Share Posted April 26, 2007 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 More sharing options...
music_lp90 Posted April 26, 2007 Share Posted April 26, 2007 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 More sharing options...
Mr_CHISOL Posted April 26, 2007 Share Posted April 26, 2007 There's several ways to do that, the one I would recommend goes like this: 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. 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 More sharing options...
primefalcon Posted April 27, 2007 Author Share Posted April 27, 2007 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.