1

i am getting this when try to send email via php mailer any buddy can help it out here is the code which i use

<?php
if(isset($_FILES['image'])){
    $errors= array();
    $file_name = $_FILES['image']['name'];
    $file_size = $_FILES['image']['size'];
    $file_tmp = $_FILES['image']['tmp_name'];
    $file_type = $_FILES['image']['type'];
    $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

    $expensions= array("jpeg","jpg","png");

    if(in_array($file_ext,$expensions)=== false){
        $errors[]="extension not allowed, please choose a JPEG or PNG file.";
    }

    if($file_size > 2097152) {
        $errors[]='File size must be excately 2 MB';
    }

    if(empty($errors)==true) {
        move_uploaded_file($file_tmp,"images/".$file_name);
        echo "Success";
    }else{
        print_r($errors);
    }
}
?>
SierraKomodo
  • 365
  • 4
  • 12
azher khan
  • 31
  • 1
  • 3
  • 2
    please show exact error message including line number – Jeff Jul 14 '17 at 18:50
  • @Jeff Ran this through my server as a test, I'm presuming this is the error he's getting: `PHP Notice: Only variables should be passed by reference in E:\sierr\Documents\Sync\sync\code\snippets-php\tmp.php on line 16` Which references this line: `$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));` – SierraKomodo Jul 14 '17 at 18:51
  • 2
    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

7

The issue is in this line:

    $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

The end() function's parameter is passed 'by reference', meaning the function directly modifies the variable that is passed into the function. Because of this, you can't pass a function directly into end() - You have to store the output of explode() into a variable first, then pass that variable into end(). Some relevant documentation:

http://php.net/manual/en/language.references.php

http://php.net/manual/en/function.end.php

This change to the above line should work:

    $file_name_array = explode('.',$_FILES['image']['name']);
    $file_ext=strtolower(end($file_name_array));
SierraKomodo
  • 365
  • 4
  • 12
2

end is a function which moves the internal array pointer to the end of the array. It does so by taking an array reference and modifying its array pointer. This line will emit a warning:

$file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

The reason it does that is because explode is a function which will return an array. PHP is just telling you that you're trying to move the array pointer but aren't keeping track of the array you're moving the pointer of.

However as a side effect end also returns the element at the end (which is what you want I'm assuming). Splitting into two lines will work:

 $array = explode('.',$_FILES['image']['name']);
 $file_ext=strtolower(end($array)); // Now a variable. 
apokryfos
  • 38,771
  • 9
  • 70
  • 114
0

see this link https://stackoverflow.com/a/4636183

Assign the result of explode to a variable and pass that variable to end:

$tmp = explode('.', $file_name);
$file_extension = end($tmp);

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.

Josef
  • 2,869
  • 2
  • 22
  • 23