LocaMobi
LocaMobi

Reputation: 1

Fix Error: Image Upload Script - Only variables should be passed by reference

Testing a file upload script I am getting this error message : Strict Standards: Only variables should be passed by reference in

Offending line 8 is: $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));

How can I fix this ?

Here is the full script I am trying to use as published at

https://www.tutorialspoint.com/php/php_file_uploading.htm

Script shown below:

<?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);
      }
   }
?>
<html>
   <body>

      <form action = "" method = "POST" enctype = "multipart/form-data">
         <input type = "file" name = "image" />
         <input type = "submit"/>

         <ul>
            <li>Sent file: <?php echo $_FILES['image']['name'];  ?>
            <li>File size: <?php echo $_FILES['image']['size'];  ?>
            <li>File type: <?php echo $_FILES['image']['type'] ?>
         </ul>

      </form>

   </body>
</html>

Upvotes: 0

Views: 1065

Answers (2)

Shuhad zaman
Shuhad zaman

Reputation: 3390

Assign the result of explode to a variable and pass that variable to end, follow this example

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

You can also use following method to get extension

$fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);

Upvotes: 0

Radu Radu
Radu Radu

Reputation: 177

You have to use a temporal variable to save the explode result

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

The simple explication is that you have to pass a variable to the "end" function not a function that returns it.

Upvotes: 0

Related Questions