2011-02-15 48 views
0

我在很多论坛上看到,在变量中列出任何$之前删除&符号(&),而且我这样做,但这样做会删除我正在使用的代码的功能。我该怎么办?不推荐使用通过引用的呼叫时间?

演示here

代码在这里:

<?php 

$val = $_GET['name']; 
$path = "./images/".$val."/"; 
$file_array = array(); 
readThisDir ($path, &$file_array); 

echo '<div class="gallery" style="display:block;" id="'.$val.'">'; 
echo '<ul>'; 
foreach ($file_array as $file) 
{ 
    if (strstr($file, "png")||strstr($file, "jpg")||strstr($file, "bmp")||strstr($file, "gif")) 
    { 
    list($width, $height) = getimagesize($file); 
    $info = exif_read_data($file);   
    echo '<li><a href="javascript:void(0);"><img src="'.$file.'" width="'.$width.'" height="'.$height.'" alt="'.$file.'"/></a><span>'.$info['Title'].'<div class="gallerynav"><a href="javascript:void(0);" class="prevproject">&laquo;</a><a href="javascript:void(0);" class="nextproject">&raquo;</a></div></span></li>'; 
    } 
} 

echo '</ul>'; 
echo '</div>'; 

    function readThisDir ($path, $arr) 
    { 
    if ($handle = opendir($path)) 
    { 
     while (false !== ($file = readdir($handle))) 
     { 
      if ($file != "." && $file != "..") 
      { 
       if (is_dir ($path."/".$file)) 
       { 
       readThisDir ($path."/".$file, &$arr); 
       } else { 
       $arr[] = $path."/".$file; 
       } 
      } 
     } 
     closedir($handle); 
    } 
    } 
?> 

回答

7

你都应该标注在函数声明的传递通过引用,而不是在函数被调用。

... 
function readThisDir ($path, &$arr) 
{ ... 
+1

,以飨读者:这回答我的直接问题,但@ircmaxwell提供了一个更好的选择。 – steve 2011-02-15 22:54:59

3

变化

function readThisDir ($path, $arr) 

function readThisDir ($path, &$arr) 

而且

readThisDir ($path."/".$file, &$arr); 

readThisDir ($path."/".$file, $arr); 

PHP不希望您直接将该变量的地址传递给该函数。

+1

是的,除了地址通常指的是指针。 [PHP中没有这样的东西。](http://php.net/manual/en/language.references.whatare.php) – netcoder 2011-02-15 22:06:33

+0

你和mrzabsky都给出了正确的答案。谢谢。 – steve 2011-02-15 22:11:13

0

使函数readThisDir返回一个包含文件信息的数组,并将其分配给$ file_array变量。 类似:

$file_array = readThisDir ($path); 

function readThisDir ($path) 
{ 
     $arr = array(); 
    if ($handle = opendir($path)) 
    { 
     while (false !== ($file = readdir($handle))) 
     { 
      if ($file != "." && $file != "..") 
      { 
       if (is_dir ($path."/".$file)) 
       { 
       readThisDir ($path."/".$file, &$arr); 
       } else { 
       $arr[] = $path."/".$file; 
       } 
      } 
     } 
     closedir($handle); 
    } 
     return $arr; 
} 
2

它不使用RecursiveDirectoryIterator直接回答你的问题,但你可以用下面的(假设5.2+)取代所有代码:

$it = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($path); 
); 
$files = array(); 
foreach ($it as $file) { 
    $files[] = $file->getPathname(); 
}