0

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

Packy
  • 3,405
  • 9
  • 50
  • 87

2 Answers2

1

This means that your end() function returns a reference to a table (the result of the function explode) triggering a php manual.

More info here: http://www.php.net/manual/en/language.references.return.php

A simple workaround would be putting it in a variable, like this then using it in the loop:

$x = end(explode(".",$file));
if(!in_array($x,$allowed_extensions)){
        $image = "http://fitter.henry-griffitts.com/fitter/images/pdf.png";
    }else{
        $image = $file;
    }
Wahib Mkadmi
  • 627
  • 2
  • 9
  • 26
  • Hmmm I see what you are saying but still getting that error. Now on the `$x = end(explode(".",$file));` line – Packy Jul 09 '15 at 04:30
0

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

}

Kanishka Panamaldeniya
  • 17,302
  • 31
  • 123
  • 193