Lewis
Lewis

Reputation: 203

Strict Standards: "Only variables should be passed by reference"

In the php function below I have on line 31 the error "Strict Standards: Only variables should be passed by reference"

As I read the answers on the forum I must change the explode(..). But I don’t have any idea how..

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

Upvotes: 0

Views: 167

Answers (3)

Thamilhan
Thamilhan

Reputation: 13323

According to the PHP Manual for end:

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.

Parameter should be sent as an array variable only.


ToDo:

Break this statement inside if:

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

to something like this:

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

Alternatively,

Since you are getting extension from the path, you can use pathinfo:

pathinfo($path)['extension']

Upvotes: 2

Chandra Kumar
Chandra Kumar

Reputation: 4205

You can try this code:

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

Upvotes: 0

yoeunes
yoeunes

Reputation: 2945

try to separate your code in the if statement like this, this should work :

// 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

Upvotes: 0

Related Questions