开发者

Call-time pass-by-reference deprecated?

I read in many forums to remove the ampersand (&) before any $ listed in a variable, and I did, but doing so removes the functionality of the code I'm using. What should I do?

Demo here.

Code 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>';
f开发者_开发问答oreach ( $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);
    }
  }
?>


You are supposed to mark the pass-by-reference in the function declaration, not where the function is called.

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


Change

function readThisDir ( $path, $arr )

To

function readThisDir ( $path, &$arr )

And

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

To

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

PHP doesn't want you to pass the address of the variable directly to the function.


It doesn't answer your question directly, but you can replace all that code with the following (assuming 5.2+) using RecursiveDirectoryIterator:

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


Make the function readThisDir to return an array with the file info populated and assign that to the $file_array variable. something like:

$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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜