I'm having trouble with this code and using different emails to view images in a directory (processed/$email) and the email changes per user's respective form entry, yet only shows the images from the most recent folder created regardless of the email given.
<form action="<?php echo $_SERVER["PHP_SELF"];?>" method="POST">
E-mail:
<input type="text" name="email" id="email2"><br>
<br>
<input type="submit" value="Retrieve" name="submit"><br><br>
</form>
and here's the PHP:
<?php
function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg', 'gif', 'png'))
{
if (substr($directory, -1) == '/') {
$directory = substr($directory, 0, -1);
}
$html = '';
if (
is_readable($directory)
&& (file_exists($directory) || is_dir($directory))
) {
$directoryList = opendir($directory);
while($file = readdir($directoryList)) {
if ($file != '.' && $file != '..') {
$path = $directory . '/' . $file;
if (is_readable($path)) {
if (is_dir($path)) {
return scanDirectoryImages($path, $exts);
}
if (
is_file($path)
&& in_array(end(explode('.', end(explode('/', $path)))), $exts)
) {
$html .= '<a href="' . $path . '"><img src="' . $path
. '" style="max-height:250px;max-width:250px" /> </a>';
}
}
}
}
closedir($directoryList);
}
return $html;
}
echo scanDirectoryImages(processed.$_POST['email2']);
?>
I've tried unsetting variables, etc. It doesn't work. When I go back to the form from any page, it's still only showing the most recently uploaded folder of images. The only thing that will make it show new images is if there is a new directory. I feel like I must be approaching this fundamentally wrong somehow and I'm new to PHP so some help would be hugely appreciated.
The original function has a recursive nature to it but doesn't utilise the existing suite of recursiveIterator
classes - hopefully the below will be of use in that respect. When I tried your original function all I got it to return was a folder name and not a list of files / images.
function scanImageDirectory( $directory, $root, $exts=array('jpg','jpeg','png','gif'), $exclusions=array('bmp') ){
$html=array();
$dirItr = new RecursiveDirectoryIterator( $directory );
$filterItr = new DirFileFilter( $dirItr, $exclusions, $directory, 'all' );
$recItr = new RecursiveIteratorIterator( $filterItr, RecursiveIteratorIterator::SELF_FIRST );
foreach( $recItr as $filepath => $info ){
if( $info->isFile() && in_array( strtolower( pathinfo( $info, PATHINFO_EXTENSION ) ), $exts ) ) {
$filename=str_replace( array( realpath( $root ), chr(92) ), array( '', chr(47) ), realpath( $info ) );
$html[]="<a href='{$filename}' target='_blank'><img src='{$filename}' alt='{$info->getFilename()}' /></a>";
}
}
return implode( PHP_EOL,$html );
}
$dir=ROOT.'/images/css/icons/browsers';
$root='c:/wwwroot';
echo scanImageDirectory( $dir, $root );
Or, as example for your situation
$dir="processed/{$_POST['email']}";
$root=$_SERVER['DOCUMENT_ROOT'];
echo scanImageDirectory( $dir, $root );
I realise that the class DirFileFilter
is one I wrote and not a native PHP class - this can only be described as an id-10-T
error.. Apologies - here is that class.
class DirFileFilter extends RecursiveFilterIterator{
protected $exclude;
protected $root;
protected $mode;
public function __construct( $iterator, array $exclude, $root, $mode='all' ){
parent::__construct( $iterator );
$this->exclude = $exclude;
$this->root = $root;
$this->mode = $mode;
}
public function accept(){
$folpath=rtrim( str_replace( $this->root, '', $this->getPathname() ), '\\' );
$ext=strtolower( pathinfo( $this->getFilename(), PATHINFO_EXTENSION ) );
switch( $this->mode ){
case 'all':
return !( in_array( $this->getFilename(), $this->exclude ) or in_array( $folpath, $this->exclude ) or in_array( $ext, $this->exclude ) );
case 'files':
return ( $this->isFile() && ( !in_array( $this->getFilename(), $this->exclude ) or !in_array( $ext, $this->exclude ) ) );
break;
case 'dirs':
case 'folders':
return ( $this->isDir() && !( in_array( $this->getFilename(), $this->exclude ) ) && !in_array( $folpath, $this->exclude ) );
break;
default:
echo 'config error: ' . $this->mode .' is not recognised';
break;
}
return false;
}
public function getChildren(){
return new self( $this->getInnerIterator()->getChildren(), $this->exclude, $this->root, $this->mode );
}
}