4

I have this code to get the extension of a file:

$extension = end(explode(".", $_FILES["rfile"]["name"]));

That is working fine on localhost, but when I upload online hosting, it is giving me this error:

Strict Standards: Only variables should be passed by reference in...

Elyor
  • 5,396
  • 8
  • 48
  • 76
  • Did you read on php.net what `end` does and what arguments it acepts? – Mjh Oct 14 '15 at 14:10
  • Possible duplicate of [Strict Standards: Only variables should be passed by reference](http://stackoverflow.com/questions/2354609/strict-standards-only-variables-should-be-passed-by-reference) – KyleK Oct 14 '15 at 14:11
  • @Mjh I haven't gone to technical parts, I just got the example from http://stackoverflow.com/a/5427229/2912339 – Elyor Oct 14 '15 at 14:17
  • That's why you should always read if a question has been marked as duplicate. As you can notice, that question is duplicate of [this one](http://stackoverflow.com/questions/4591523/whats-the-best-way-practice-to-get-the-extension-of-a-uploaded-file-in-php) which provides the accurate answer. Question you linked is not accurate and shouldn't be used. Sadly, it attracted many visits :/ – Mjh Oct 14 '15 at 14:23
  • Possible duplicate of [Only variables should be passed by reference](https://stackoverflow.com/q/4636166/1255289) – miken32 Oct 11 '17 at 22:00

3 Answers3

8

Why not use pathinfo (PHP >= 4.0.3), i.e.:

$ext = pathinfo($_FILES["rfile"]["name"])['extension'];

Live PHP demo

http://ideone.com/eMpbnL

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
7

PHP end takes an reference to a variable as argument. http://php.net/manual/en/function.end.php

So, with strict standards enabled, you should put the result of explode into a variable first:

$exp = explode(".", $_FILES["rfile"]["name"])
$extension = end($exp);
dognose
  • 20,360
  • 9
  • 61
  • 107
2

Your localhost is on an old PHP version or is not configured to display strict standards errors.

Now in PHP, you should do:

$explode = explode(".", $_FILES["rfile"]["name"]);
$extension = end($explode);

See the example in the doc: http://php.net/manual/en/function.end.php#refsect1-function.end-examples

KyleK
  • 4,643
  • 17
  • 33