Akram
Akram

Reputation: 493

Uploading image to server symfony2

I want to upload an image file to a directory on the server.I use those lines to do it when I use Symfony form builder.

    $file = $form->get("picture")->getData();
    $name = $file->getClientOriginalName();
    $dir = __DIR__.'/../../../../web/uploads/documents';
    $file->move($dir,$name) ;

<input type="file" name="{{ form.picture.vars.full_name }}" id="{{ form.picture.vars.id }}"/>

I'm asking how to achieve that if i'm not using Symfony forms.

HTML:

<input type="file" name="picture" id="picture"/>

and I changed php to:

    $file = $requst->get("picture");
    $name = $file->getClientOriginalName();
    $dir = __DIR__.'/../../../../web/uploads/documents';
    $file->move($dir,$name) ;

I get an error saying: call function move() on non member object.

Upvotes: 0

Views: 956

Answers (1)

kapa89
kapa89

Reputation: 615

Try to use this code:

$files = $request->files->all();

In array $files you can find your picture. Use this file for move.

$file = $files['key_of_picture'];
$file->move($directory, $name);

Upvotes: 2

Related Questions