0

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>';
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);
    }
  }
?>
steve
  • 688
  • 6
  • 13
  • 32

4 Answers4

7

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

...
function readThisDir ( $path, &$arr )
{ ...
Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
  • 1
    To readers: This answers my question directly, but @ircmaxwell gave a better alternative. – steve Feb 15 '11 at 22:54
3

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.

Josh
  • 12,448
  • 10
  • 74
  • 118
  • 1
    True, except that addresses usually refer to pointers. [There are no such thing in PHP.](http://php.net/manual/en/language.references.whatare.php) – netcoder Feb 15 '11 at 22:06
2

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();
}
ircmaxell
  • 163,128
  • 34
  • 264
  • 314
  • Could you be more specific? I'm getting an error in PHP validation with just the first two lines. If you can, could you just convert the code I already have above (with all divs and uls) and paste it here/in a pastebin? I'm still learning PHP so it would be easier for me to look over the complete code and see how it works. – steve Feb 15 '11 at 22:43
  • @steve: replace `$file_array = array (); readThisDir ( $path, &$file_array );` with the code above (just change `$files` to `$file_array`. And get rid of that function. You should be set from there... – ircmaxell Feb 15 '11 at 22:49
  • Wow, excellent. I did that before and it didn't work properly, but PHP gave me the problem: the semicolon on the second line apparently isn't supposed to be there. Thanks a lot, that clears up my code quite a bit. I was wondering if there was some way to iterate through all directories without having to do all that. Thanks again. – steve Feb 15 '11 at 22:52
0

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;
}
Chandu
  • 81,493
  • 19
  • 133
  • 134