1

I'm using laravel as the backend of my application, the front end allows the user to enter the first name aswell as uploading a photo of themselves.. Using html5 they can crop the photo on page and the cropped photo is sent on form submit to my controller..

I'm getting the following error:

ErrorException in testcontroller.php line 31:
Only variables should be passed by reference

Here's my View which contains the form and the function for on page crop:

<form enctype="multipart/form-data" action="/testing" method="post" role="form">
   <input type="hidden" name="_token" value="{{ csrf_token() }}">
   <div class="form-group">
      <label for="name">Name</label>
         <input type="text" name="name" class="form-control" />
   </div>

   <div class="form-group">
      <label for="name">Title</label>
        <input type="text" name="title" class="form-control" />
   </div>


   <div class="form-group">
      <label>Image</label>
        <div class="dropzone" data-width="960" data-height="540" data-ajax="false" data-originalsave="true" style="width: 100%;">
         <input type="file" name="thumb" required="required" />
        </div>
   </div>

   <button type="submit" class="btn btn-default">Submit</button>

</form>

Here's my Controller which saves the photo locally and eventually update the customers record with the url to the file on my server:

public function test(Request $request)
    {
        $error                    = false;

        $absolutedir            = dirname(__FILE__);
        $dir                    = '/tmp/';
        $serverdir              = $absolutedir.$dir;
        $filename               = array();

        foreach($_FILES as $name => $value) {
            $json                   = json_decode($_POST[$name.'_values']);
            $tmp                    = explode(',',$json->data);
            $imgdata                = base64_decode($tmp[1]);

LINE 31 $extension = strtolower(end(explode('.',$json->name)));

            $fname                  = substr($json->name,0,-(strlen($extension) + 1)).'.'.substr(sha1(time()),0,6).'.'.$extension;


            $handle                 = fopen($serverdir.$fname,'w');
            fwrite($handle, $imgdata);
            fclose($handle);

            $filename[]             = $fname;
        }  

        return view('upload');
    }

I'm getting the following error:

ErrorException in testcontroller.php line 31:
Only variables should be passed by reference

LINE 31 $extension = strtolower(end(explode('.',$json->name)));

Qazi
  • 5,015
  • 8
  • 42
  • 62
Dev.W
  • 2,340
  • 9
  • 38
  • 77

2 Answers2

3

The reason is because of the end method requires a reference to an existing array.

So just expand the code.

$fileAry = explode('.',$json->name);
$extension = strtolower(end( $fileAry ));

Also you may want to take a look at the pathinfo method for grabbing a file extension.

Steve Parish
  • 1,824
  • 2
  • 14
  • 12
0

Probably this will help you:

Only variables should be passed by reference

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.

So instead of:

$extension = strtolower(end(explode('.',$json->name)));

try to use:

$temp = explode('.',$json->name);
$extension = strtolower(end($temp));
Community
  • 1
  • 1
Tim L
  • 165
  • 1
  • 6