【问题标题】:How to fix "Only variables should be passed by reference" error如何修复“只能通过引用传递变量”错误
【发布时间】:2014-10-27 05:20:30
【问题描述】:

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

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

代码:

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

【问题讨论】:

标签: php scandir


【解决方案1】:

试试这个

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

【讨论】:

    【解决方案2】:

    试试这个:

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

    【讨论】:

      【解决方案3】:

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

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

      见:(<5.6) Occasionally significant parentheses

      【讨论】:

        【解决方案4】:

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

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

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

        $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
        

        【讨论】:

          猜你喜欢
          • 2018-03-09
          • 2014-02-02
          • 2012-04-08
          • 2015-02-02
          • 2017-07-08
          • 2012-10-25
          • 1970-01-01
          • 2013-06-21
          • 1970-01-01
          相关资源
          最近更新 更多