dounaidu0204 2015-07-09 04:09
浏览 70
已采纳

错误只应通过引用传递变量

I am getting an error I have not seen on a progam I wrote a while ago. Not sure if the php version changed cause this or not. I am pretty basic when it comes to PHP so any insight would be great. Basically I am just getting some files from a folder, adding the image of the file if there is one (if not use stock image), then list them files.

`

  $allowed_extensions = array("gif", "jpeg", "jpg", "png");
    $files = glob('files/*');

    natcasesort($files);
    foreach($files as $file){
    if(!in_array(end(explode(".",$file)),$allowed_extensions)){
        $image = "http://fitter.henry-griffitts.com/fitter/images/pdf.png";
    }else{
        $image = $file;
    }
    echo '<div class="one_half"><img src="' . $image . '" class="images" /></br><h2>' .basename($file). '</h2><a class="download" href="http://fitter.henry-griffitts.com/fitter/download.php?file='.base64_encode($file).'"></a></div>';
}

?>

The error says its on this line if(!in_array(end(explode(".",$file)),$allowed_extensions)){

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

  • 写回答

2条回答 默认 最新

  • dongxiezhuo8852 2015-07-09 04:43
    关注

    Try this code

    The problem is, that end requires a reference, because it modifies the internal representation of the array (i.e. it makes the current element pointer point to the last element).

    The result of explode('.', $file_name) cannot be turned into a reference. This is a restriction in the PHP language, that probably exists for simplicity reasons.

    foreach($files as $file){
    
      $exploded_file = explode(".",$file);
    
      if(!in_array(end($exploded_file),$allowed_extensions)){
        $image = "http://fitter.henry-griffitts.com/fitter/images/pdf.png";
      }else{
        $image = $file;
      }
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?