1

I know, that there are many many cases about this theme already, but I looked them through, and could not find desired. Also I noticed that not a lot of the users got their answer. I am working with Laravel5, and I'm trying to upload a picture. Simple upload, just save any picture in public/img folder. I have looked up some tutorials and came up with this code: View form:

<form action="{{URL::to('adminPanel/addImg/done/'.$projectId)}}" method="get" enctype="multipart/form-data">
        <input name="image" type="file" />
        <br><br>
        <input type="submit" value="Ielādēt"/>
</form>

And the controller code:

public function addImageDone($id) {
         $file            = Input::file('image');
         $destinationPath = public_path().'/img/';
         $filename        = $id;
         $file->move($destinationPath);   
    }

I keep getting this error :

Call to a member function move() on a non-object

And I am sure, that the chosen file is image.

I would appreciate any help

So its done, the main issue was the POST part! Also the file format, but here are the correct code, that adds the image: form:

<form method="POST" action="{!! URL::to('adminPanel/addImg/done/'.$projectId) !!}" accept-charset="UTF-8" enctype="multipart/form-data">
        <input type="hidden" name="_token" value="{{ csrf_token() }}"> //this part is to make POST method work
        <input name="image" type="file" />
        <br><br>
        <input type="submit" value="Ielādēt"/>
    </form> 

controller:

public function addImageDone($id) {
         $file = Input::file('image');
         $destinationPath = public_path().'/img/';
         $file->move($destinationPath, $id.'.png');            
    }
Oskars
  • 407
  • 4
  • 24
  • 1
    You have method="get" instead of method="POST" also try doing a `dd(Input::file('image'))` and check if your form is really submitting your image – Fabio Antunes Apr 23 '15 at 14:19
  • is POST method obligatory in uploading images? Because I thought i could use get instead, because I am making new view after the upload with the same controller function... – Oskars Apr 23 '15 at 14:38
  • also I'm not really sure why, but it gives me an token mismatch error each time I try to sue POST method – Oskars Apr 23 '15 at 14:39
  • because you have the middleware responsible CSRF protection active, you should read Laravel Documentation http://laravel.com/docs/master/routing#csrf-protection – Fabio Antunes Apr 23 '15 at 14:43
  • Also this why you can't submit files using GET method http://stackoverflow.com/questions/15201976/file-uploading-using-get-method – Fabio Antunes Apr 23 '15 at 14:44
  • Thanks, turns out I had to just add one silly row "" to my form, and POST methods started working, but u already knew it. Now, if i run my code with post method, I'm creating weird fille in the destination folder instead of the image. The file, that shoves up is: phpXXXX.tmp Also the thing is that I have to pass the id from the view to controller to be able name the image file by it. – Oskars Apr 23 '15 at 14:54
  • Once again try reading the Files documentation it's pretty simple http://laravel.com/docs/5.0/requests#files if after that you can't make it work I suggest that you put a little effort on this question by editing and posting your controller code and other useful info – Fabio Antunes Apr 23 '15 at 15:01

2 Answers2

1

I don't know if you are using Laravel 4.2 or 5.0. But..

I recommend you to use illuminate/html - Form class. Try to use POST instead GET to upload files (https://stackoverflow.com/a/15210810/781251, http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.1.1 , http://php.net/manual/en/features.file-upload.post-method.php , http://php.net/manual/en/features.file-upload.php)

If Laravel 4.2:

View:

{{ Form::open(['url'=>'adminPanel/addImg/done' . $projectId , 'files' => true , 'method' => 'POST']) }}

    <label for="file">File</label>   
    {{ Form::file('file') }}

{{ Form::close() }}

Controller:

public function postImage()
{
    if( ( $file = Input::file('file') ) != null && $file->isValid() )
    {
        $file->move('destination','my_file_new_name.extension');

        return Redirect::back();
    }

    throw new \Exception('Error while upload file');
}

Laravel 5.0

View:

{!! Form::open(['url'=>'adminPanel/addImg/done' . $projectId , 'files' => true , 'method' => 'POST']) !!}

    <label for="file">File</label>   
    {!! Form::file('file') !!}

{!! Form::close() !!}

Controller:

public function upload(Request $request)
{
    if( ( $file = $request->file('file') ) != null && $file->isValid() )
    {
        $file->move('destination','my_file_new_name.extension');

        return redirect()->back();
    }

    throw new \Exception('Error while upload file');
}

To create a file with a new name and keep the extension:

$ext = $file->getClientOriginalExtension();
$newName = str_random(20) . '.' . $ext;

$file->move( storage_path('images') , $newName );

Now, you have a new name with the same extension. To validate if your file is an image or..whatever, use Validator.

Community
  • 1
  • 1
Eduardo Stuart
  • 2,869
  • 18
  • 22
  • I have a question about extension of the file part. I got the upload, but it uploads the file as a unrecognized file. And I'm sure that its the image, because if I open it as a, for example, bmp file, it open the image. how can i set the file format of the image? – Oskars Apr 23 '15 at 15:04
  • 1
    You can create a new name and grab the "client extension". I'll update my answer. – Eduardo Stuart Apr 23 '15 at 15:05
  • Thank you, the problem wasn't with the image name, it was with the format. But thanks, because of your answer, I got the correct result. Just that I was unfamiliar with he syntax a bit.. $file->move($destinationPath, $id.'.png'); there has to be a dot before png ans also another one right after $id – Oskars Apr 23 '15 at 15:10
0

Try this

<form method="POST" action="{!! URL::to('adminPanel/addImg/done/'.$projectId) !!}" accept-charset="UTF-8" enctype="multipart/form-data">
            <input name="image" type="file" />
            <br><br>
            <input type="submit" value="Ielādēt"/>
</form> 

And get the image as

public function postImage(Request $request) {
    $image = $request->file("image");
Margus Pala
  • 8,433
  • 8
  • 42
  • 52