只有变量可以通过引用错误传递

lap*_*ete 8 php arrays reference

第197行的脚本'/usr/local/apache2/htdocs/read.php'发生错误:只应通过引用传递变量(第196行$ext = strtolower(array_pop(explode('.',$filename)));)

if(!function_exists('mime_content_type')) {

    function mime_content_type($filename) {

        $mime_types = array(

            'txt' => 'text/plain',
            'htm' => 'text/html',
            'html' => 'text/html', //ETC

        );

        $ext = strtolower(array_pop(explode('.',$filename)));
        if (array_key_exists($ext, $mime_types)) {
            return $mime_types[$ext];
        }
        elseif (function_exists('finfo_open')) {
            $finfo = finfo_open(FILEINFO_MIME);
            $mimetype = finfo_file($finfo, $filename);
            finfo_close($finfo);
            return $mimetype;
        }
        else {
            return 'application/octet-stream';
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我正在使用来自http://php.net/manual/en/function.mime-content-type.php的这个小脚本,虽然我遇到了一个我似乎无法弄清楚的致命错误.是否有任何人有这方面的经验并提出一些指示或指出我正确的方向?

Joh*_*hnP 10

在传递之前,您需要将explode()的结果变为变量

$var = explode('.',$filename);
$ext = strtolower(array_pop($var));
Run Code Online (Sandbox Code Playgroud)


T.J*_*der 7

该代码将explode函数的结果(值)传递给array_pop,但是array_pop期望数组变量(通过引用),而不是值.(该&array_pop声明告诉我们,它的预期接受的参考.)

您可以通过使用数组变量来存储结果explode,然后将其传递给它来修复它array_pop.