IamTomCat Posted January 24, 2015 Share Posted January 24, 2015 Hi I echo out images from a folder. The problem is that I have a html file in the same folder but I don't want to echo that out. How can I write my code to get rid of the html in the output? Here my code: <?php $filetype = '*.html'; $dirname = substr('$filetype'); $i=0; if (isset($_POST['submit2'])) { //get the dir from POST $selected_dir = $_POST['myDirs']; //now get the files from within the selected dir and echo them to the screen foreach(glob($selected_dir . DIRECTORY_SEPARATOR . '*') as $dirname) { echo substr($dirname, 0, -4); echo '<img src="'.$dirname.'" />'; echo "<label><div class=\"radiobt\"><input type='radio' name='radio1' value='$i'/></div></label>"; } } ?> P.S. when I echo out : substr($dirname, 0, -4); I want to get ride of the html filename there too. How can I do so something like this? Quote Link to comment https://forums.phpfreaks.com/topic/294190-how-to-get-rid-of-html-file-when-echo-out-images-from-folder/ Share on other sites More sharing options...
CroNiX Posted January 24, 2015 Share Posted January 24, 2015 (edited) //does this file contain "html" as the last 4 characters? if (strtolower(substr($dirname, -4)) != 'html') { //this isn't a file with a html extension } Further, you can use glob to ONLY get files that have certain file extensions by using the GLOB_BRACE flag, so it wouldn't grab the html file to begin with. foreach(glob($selected_dir . DIRECTORY_SEPARATOR . '*.{jpg,png}', GLOB_BRACE) as $dirname) //will only get files with .jpg and .png Edited January 24, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/294190-how-to-get-rid-of-html-file-when-echo-out-images-from-folder/#findComment-1504048 Share on other sites More sharing options...
ginerjm Posted January 24, 2015 Share Posted January 24, 2015 Also - this line is not what (I think) you intend it to be: $filetype = '*.html'; $dirname = substr('$filetype'); $dirname is going to contain exactly: $filetype It is NOT going to contain anything with *.html (or any part of it). Substr requires a start value and you left it out. If you had turned on error checking (As You Should in development) you would be seeing a warning. Quote Link to comment https://forums.phpfreaks.com/topic/294190-how-to-get-rid-of-html-file-when-echo-out-images-from-folder/#findComment-1504063 Share on other sites More sharing options...
IamTomCat Posted January 24, 2015 Author Share Posted January 24, 2015 Hi It is brilliant. You are a genius. Thank you so much Guru. P.S. foreach(glob($selected_dir . DIRECTORY_SEPARATOR . '*.{jpg,png}', GLOB_BRACE) as $dirname) //will only get files with .jpg and .png This code was exactly what I have been looking for Quote Link to comment https://forums.phpfreaks.com/topic/294190-how-to-get-rid-of-html-file-when-echo-out-images-from-folder/#findComment-1504081 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.