如何解决"只有变量应该通过引用传递"错误

时间:2014-10-27 05:20:31

标签: php scandir

我正在尝试显示文件夹中的图像。当我运行我的脚本时,我得到以下错误 -

"Only variables should be passed by reference in C:\wamp\www\salon\mainS\image_delete.php on line 72"

CODE:

<?php

    $dir = 'uploads/';
    $display = array('jpg', 'jpeg', 'png', 'gif');

    if (file_exists($dir) == false) {
      echo 'Directory \''. $dir. '\' not found!';
    } else {
    $dir_contents = scandir($dir);

    foreach ($dir_contents as $file) {
      $type = strtolower(end(explode('.', $file)));

      if ($file !== '.' && $file !== '..' && in_array($type, $display) == true)     
      {             
        echo'<div style="width:1170px;" >'.
        '<div style="float:left; margin-right:5px;" >'.'<img style="width:200px; height:200px;"src="'. $dir. '/'. $file. '" alt="'. $file.'"/>'.'</div>'
        .'</div>';
      }
    }
    }
?>

我的第72行是

$type = strtolower(end(explode('.', $file)));

4 个答案:

答案 0 :(得分:1)

试试这个

$name_parts = explode('.', $file);
$type = strtolower(end($name_parts));

答案 1 :(得分:0)

试试这个:

    $dir = 'uploads/';
    if (file_exists($dir) == false) {
    echo 'Directory \''. $dir. '\' not found!';
    } else {
        $dir_contents = scandir($dir);

        foreach ($dir_contents as $file) {
            try {
                if (!is_dir($file) && (getimagesize($file))) {
                    echo'<div style="width:1170px;" >' .
                    '<div style="float:left; margin-right:5px;" >' . '<img style="width:200px; height:200px;"src="' . $dir . '/' . $file . '" alt="' . $file . '"/>' . '</div>'
                    . '</div>' . "\r\n";
                }
            } catch (Exception $e) {
                continue;
            }
        }
    }

$dir = 'uploads/'; if (file_exists($dir) == false) { echo 'Directory \''. $dir. '\' not found!'; } else { $dir_contents = scandir($dir); foreach ($dir_contents as $file) { try { if (!is_dir($file) && (getimagesize($file))) { echo'<div style="width:1170px;" >' . '<div style="float:left; margin-right:5px;" >' . '<img style="width:200px; height:200px;"src="' . $dir . '/' . $file . '" alt="' . $file . '"/>' . '</div>' . '</div>' . "\r\n"; } } catch (Exception $e) { continue; } } }

答案 2 :(得分:0)

通过添加额外的括号来修复它,例如:

$type = strtolower(end((explode('.', $file))));

请参阅:(<5.6) Occasionally significant parentheses

答案 3 :(得分:0)

将explode的结果分配给一个临时变量,并在最后传递该变量,这是一个示例。

$tmp = explode('.', $fileName);
$fileExtension = end($tmp);

还可以使用以下方法来获得扩展名

$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);