0

I'm trying to display images from folder. When I run my script I get following error -

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

my line 72 is

$type = strtolower(end(explode('.', $file)));
Jenz
  • 8,280
  • 7
  • 44
  • 77
Tje
  • 71
  • 1
  • 3
  • 15
  • dump `$file` and see what you are getting. – Sougata Bose Oct 27 '14 at 05:26
  • Your code is working fine for me ... check in image_delete.php file if there is any other issue – TBI Oct 27 '14 at 05:34
  • 1
    Possible duplicate of [Only variables should be passed by reference](http://stackoverflow.com/questions/4636166/only-variables-should-be-passed-by-reference) – kenorb Nov 14 '15 at 16:00

4 Answers4

1

try this

$name_parts = explode('.', $file);
$type = strtolower(end($name_parts));
Syed Arif Iqbal
  • 1,830
  • 12
  • 16
0

Try this:

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

the_wizard
  • 477
  • 5
  • 9
0

Fix it by adding extra bracket, e.g.:

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

See: (<5.6) Occasionally significant parentheses

kenorb
  • 155,785
  • 88
  • 678
  • 743
0

Assign the result of explode to a temporary variable and pass that variable at end, here is an example.

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

also to get extension you can also use following method

$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
Shuhad zaman
  • 3,156
  • 32
  • 32