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');            
    }
7
  • 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 Commented Apr 23, 2015 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... Commented Apr 23, 2015 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 Commented Apr 23, 2015 at 14:39
  • because you have the middleware responsible CSRF protection active, you should read Laravel Documentation laravel.com/docs/master/routing#csrf-protection Commented Apr 23, 2015 at 14:43
  • Also this why you can't submit files using GET method stackoverflow.com/questions/15201976/… Commented Apr 23, 2015 at 14:44

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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?
You can create a new name and grab the "client extension". I'll update my answer.
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
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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.