0

Possible Duplicate:
Strict Standards php error

Hi have a problem with a upload script in php. I am trying to make it accept only images but i get an error and i dont know how to fix it. It works fine when i remove the only image part. I hope someone can help me :)

The error code I get:

Strict Standards: Only variables should be passed by reference in C:\xampp\htdocs\upload_file.php on line 3 Invalid file

The upload_file.php code:

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

Thanks in advance!

Community
  • 1
  • 1

1 Answers1

4

You can't call end() on the result of explode(), because it expects a variable as first parameter.

Per the documentation:

The array. This array is passed by reference because it is modified by the function. This means you must pass it a real variable and not a function returning an array because only actual variables may be passed by reference.

Do it like this:

$foo = explode('.', $_FILES['file']['name']);
$extension = end($foo);

A different approach would be to deactivate the strict error checking, but I guess removing the error would be preferable.

kittycat
  • 14,983
  • 9
  • 55
  • 80
Till Helge
  • 9,253
  • 2
  • 40
  • 56
  • Well...I guess `end()` is defined to be working with a reference...and the result of `explode()` isn't a reference...only a variable can contain a reference. Or something along that line. – Till Helge Jan 26 '13 at 09:08
  • I just tried it and it appears to be that way. When using a temporary variable, there is no strict notice. Probably some weird PHP quirk again. ;) – Till Helge Jan 26 '13 at 09:13
  • apparently its in the documentation, added to your answer. I rarely have ever used `end()` so never knew that. – kittycat Jan 26 '13 at 09:14
  • Hey thanks for your answer! but now it doesnt accept any picture, it always gives the error: Invalid file – iliasselkaddouri Jan 26 '13 at 10:13
  • I guess i will remove the restriction – iliasselkaddouri Jan 26 '13 at 10:13