-->

Only Variables can be passed by reference error

2019-01-15 19:15发布

问题:

An error occurred in script '/usr/local/apache2/htdocs/read.php' on line 197: Only variables should be passed by reference (line 196 is $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';
        }
    }
}

I'm using this little script from http://php.net/manual/en/function.mime-content-type.php, though I'm getting a fatal error I can't seem to figure out. Does anyone that has experience with this and shed some light or point me in the right direction?

回答1:

You need to make the result of explode() a variable before you pass it on

$var = explode('.',$filename);
$ext = strtolower(array_pop($var));


回答2:

That code is passing the result of the explode function (a value) into array_pop, but array_pop expects an array variable (by reference), not a value. (The & in the array_pop declaration tells us that it's expecting to accept a reference.)

You can fix it by using an array variable to store the result of explode, and then passing that into array_pop.