Jump to content

How to get rid of .html file when echo out images from folder


IamTomCat

Recommended Posts

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?

//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

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.

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 :happy-04:

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.