【问题标题】:Strict Standards: "Only variables should be passed by reference"严格标准:“只能通过引用传递变量”
【发布时间】:2017-08-02 10:41:14
【问题描述】:

在下面的 php 函数中,我在第 31 行出现错误“严格标准:只能通过引用传递变量”

当我阅读论坛上的答案时,我必须更改explode(..)。但我不知道如何..

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

【问题讨论】:

  • end(explode('/', $path)) 设置为 if 语句上方的自己的变量,然后改用它。

标签: php


【解决方案1】:

根据PHP Manualend

这个数组是通过引用传递的,因为它被函数修改了。这意味着您必须传递一个实变量而不是返回数组的函数,因为只有实变量可以通过引用传递。

参数只能作为数组变量发送。


待办事项:

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']

【讨论】:

    【解决方案2】:

    你可以试试这个代码:

    <?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;
    }
    

    【讨论】:

    • $path_info = pathinfo($path);给出解析错误:语法错误,意外';'
    【解决方案3】:

    尝试像这样在 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
    

    【讨论】:

      最近更新 更多