Jump to content

Recommended Posts

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";
?>

 

Link to comment
https://forums.phpfreaks.com/topic/75473-show-images-only/
Share on other sites

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





} 
?>

Link to comment
https://forums.phpfreaks.com/topic/75473-show-images-only/#findComment-381796
Share on other sites

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 />";
} 
?>

Link to comment
https://forums.phpfreaks.com/topic/75473-show-images-only/#findComment-381802
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.