Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only Variables can be passed by reference error

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?

like image 820
laphiloctete Avatar asked Jun 20 '11 10:06

laphiloctete


2 Answers

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

$var = explode('.',$filename);
$ext = strtolower(array_pop($var));
like image 82
JohnP Avatar answered Nov 10 '22 05:11

JohnP


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.

like image 41
T.J. Crowder Avatar answered Nov 10 '22 07:11

T.J. Crowder