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">«</a><a href="javascript:void(0);" class="nextproject">»</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;
}
精彩评论