emediastudios Posted October 31, 2007 Share Posted October 31, 2007 I have a code that displays all the files in a folder as well as a thumbnail image. This is a page that allows my client the option to delete a image from his dir if it is no longer needed. I was hoping that i could alter my code so that it only displayed images in jpg, jpeg, gif or png. and if there is files or folders that are not of this format in the selected(../images) directory, not echo them. Here is my code. <?PHP $folder = "../images/"; $handle = opendir($folder); # Making an array containing the files in the current directory: while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } closedir($handle); #echo the files foreach ($files as $file) { echo "<img src='$folder$file' alt='image' width='150' /><br />"; echo "<a href='$folder$file'>$file</a> - "; echo "<a href=filedelete.php?file=$file>Delete this Image</a><br /><hr />"; } ?> MAny thanks. Oh i have this code too, maybe i could use some code out of it and combine it with the upper code? <?php echo "<select name=\"file\">\n"; foreach (new DirectoryIterator('.') as $file) { // if the file is not this file, and does not start with a '.' or '..', // then store it for later display if ( (!$file->isDot()) && ($file->getFilename() != basename($_SERVER['PHP_SELF'])) ) { echo "<option>"; // if the element is a directory add to the file name "(Dir)" echo ($file->isDir()) ? "(Dir) ".$file->getFilename() : $file->getFilename(); echo "</option>\n"; } } echo "</select>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/75473-show-images-only/ Share on other sites More sharing options...
emediastudios Posted October 31, 2007 Author Share Posted October 31, 2007 I have imported a function into the code but am unsure how to call upon it <?PHP //filter extensions function valid_ext($file_name) { $valid = array("jpeg","jpg","png","gif"); $extension = strtolower(substr($file_name,-3,3)); return (in_array($extension, $valid)); } ///Directory Settings $folder = "../images/"; $handle = opendir($folder); # Making an array containing the files in the current directory: while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } closedir($handle); #echo the files foreach ($files as $file) { echo "<img src='$folder$file' alt='image' width='150' /><br />"; echo "<a href='$folder$file'>$file</a> - "; echo "<a href=filedelete.php?file=$file>Delete this Image</a><br /><hr />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75473-show-images-only/#findComment-381796 Share on other sites More sharing options...
emediastudios Posted October 31, 2007 Author Share Posted October 31, 2007 I have no idea, i dont get any errors, just a blank page. I added a filter and array butim sure its all wrong. Heres my new code. <?php //filter extensions $file_name = $file; function valid_ext($file_name) { $valid = array("jpeg","jpg","png","gif"); $extension = strtolower(substr($file_name,-3,3)); return (in_array($extension, $valid)); } ///Directory Settings $folder = "../images/"; $handle = opendir($folder); # Making an array containing the files in the current directory: while (false !== ($file = readdir($handle))) { if ($file != '.' && $file != '..') $files[] = $file; } closedir($handle); //Test Check $Valid = false; if(valid_ext($file_name)) #echo the files foreach ($files as $file) { echo "<img src='$folder$file' alt='image' width='150' /><br />"; echo "<a href='$folder$file'>$file</a> - "; echo "<a href=filedelete.php?file=$file>Delete this Image</a><br /><hr />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75473-show-images-only/#findComment-381802 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.