Jump to content
IamTomCat

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

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?

Share this post


Link to post
Share on other sites
//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 by CroNiX

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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:

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • 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.