2016-07-31 161 views
0

我在使用此代码时遇到问题,并使用不同的电子邮件来查看目录中的图像(已处理/ $ email)和电子邮件对每个用户各自表单条目的更改,但仅显示来自不管给定的电子邮件是否创建最近的文件夹。

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

和这里的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']); 
    ?> 

我试过解封变量等,这是行不通的。当我从任何页面返回表单时,它仍然只显示最近上传的图像文件夹。唯一能够显示新图像的是如果有新目录。我觉得我必须以某种方式接近这个根本错误,我是PHP的新手,所以一些帮助将非常感激。

+0

你有'id'和'name'混淆〜你应该在发布的变量中使用'name' ... – RamRaider

回答

0

原始函数具有递归性质,但不使用recursiveIterator类的现有套件 - 希望以下内容可以在这方面使用。当我尝试你的原始功能时,所有我得到它返回的是文件夹名称而不是文件/图像列表。

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); 

或者,例如为您的具体情况

$dir="processed/{$_POST['email']}"; 
$root=$_SERVER['DOCUMENT_ROOT']; 
echo scanImageDirectory($dir, $root); 

我意识到类DirFileFilter是我写的,而不是原生的PHP类 - 这只能被描述为id-10-T错误..道歉 - 这是那堂课。

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); 
    } 
} 
+0

非常感谢!我有点困惑,因为你有一个未使用的变量($ dir)。我也尝试过你的功能,并且它不会显示图像到我修改其他变量后输入到表单中的图像(你在编辑它们时编辑了这些图像!) –

+0

我编辑它是因为我发现了代码中的错误 - 它花了一段时间,因为我仍然半睡着了,但你使用的是id而不是名称(POST vars使用名称) - 我有这个工作可以建立一个目录结构来容纳电子邮件地址等 – RamRaider

+0

仍然无法在http: //98.117.55.190/myimageshandle.php以及使用你的代码的表单下面的所有东西都不见了(应该是下面的一些段落,你可以在其他页面上看到),大声笑 http://i.imgur.com/F0yjsbS.png是我现在看到的,我在使用'$ directory'而不是'$ dir'函数调用 –