2017-08-02 122 views
0

在PHP函数“只有变量应该按引用传递”下面我有31行错误“严格标准:只有变量应该按引用传递”严格的标准:

当我读到的答案论坛我必须改变爆炸(..)。但我没有任何想法如何..

function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg', 'png')) 
{ 
    if (substr($directory, -1) == 'uploads/') { 
     $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) // Line 31 
        ) { 
         $html .= '<a href="' . $path . '"><img src="' . $path 
          . '" style="max-height:100px;max-width:100px" /></a>'; 
        } 
       } 
      } 
     } 
     closedir($directoryList); 
    } 
    return $html; 
} 
+0

设置'结束(爆炸('/ ',$ path))'将它自己的变量放在if语句之上,然后使用它。 –

回答

0

你可以试试这个代码:

<?php 

<?php 

function scanDirectoryImages($directory, array $exts = array('jpeg', 'jpg', 'png')) 
{ 
    if (substr($directory, -1) == 'uploads/') { 
     $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); 
        } 

        $path_info = pathinfo($path); 
        $ext = strtolower($path_info['extension']); 

        if (is_file($path) && in_array($ext, $exts)) { 
         $html .= '<a href="' . $path . '"><img src="' . $path 
          . '" style="max-height:100px;max-width:100px" /></a>'; 
        } 
       } 
      } 
     } 
     closedir($directoryList); 
    } 
    return $html; 
} 
+0

$ path_info = pathinfo($ path);给解析错误:语法错误,意外的';' – Lewis

+0

再次更新您的代码。 –

+0

是的,它现在可以工作。感谢你的帮助。 – Lewis

0

尝试在这样的if语句分开你的代码,这应该工作:

// other code 

if (is_dir($path)) { 
    return scanDirectoryImages($path, $exts); 
} 

$explode_1 = explode('/', $path); 
$explode_2 = explode('.', end($explode_1)); 

if (is_file($path) && in_array(end($explode_2), $exts)) { 

// the rest of the code 
2

按照PHP Manualend

This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.

参数应仅作为数组变量发送。


TODO:

打破了这个说法里面if

is_file($path) && in_array(end(explode('.', end(explode('/', $path)))), $exts) 

到这样的事情:

$path1 = explode('/', $path); 
$path2 = end($path1); 
$path3 = explode('.', $path1); 
$path4 = end($path3); 
is_file($path) && in_array($path4, $exts) 

另外,

因为你是从路径得到扩展,可以使用pathinfo

pathinfo($path)['extension']