I am trying to upload file with Symfony3 but with no luck. I have a Profile entity which is linked to User entity with 1-1 relationship. The profile contains a picture column. I have created a ProfileType and Profile Model. Upon submitting the form, the model contains only the File name and nothing else. The $_FILES array is also empty. This is the code.
$builder
->add("name", TextType::class, array(
"required" => true,
))
->add("email", EmailType::class, array(
"required" => true,
))
->add("city", TextType::class, array(
"required" => false,
))
->add("country", ChoiceType::class, array(
"required" => false,
))
->add("picture", FileType::class, array(
"required" => false,
));
class ProfileModel
{
private $name;
private $email;
private $city;
private $country;
private $picture;
In Controller I am creating the form like this.
$profileForm = $this->createForm(ProfileType::class, $profileModel);
When I get the picture, It contains just the name.
$file = $profileForm->get("picture")->getData();
You should try
$form = $this->createForm(ProfileType::class, $profileModel); $form->handleRequest($request); $file = $profileModel->getBrochure();
More: http://symfony.com/doc/current/controller/upload_file.html
Hewwo rashidkhan~
Symfony doc is quite complete on the upload process, did you read it?
http://symfony.com/doc/current/controller/upload_file.html
After a few modifications, I choose to use it as service. Here is the process:
1) Add a few parameters to
app/config/config.yml
:under
parameters
:under
twig
The two
profile_directory
added just now will be used as variables in both your upload service and twig to point the targer directory. I addedanother_directory
to explain something more a bit after.2) Create the service:
Create a new file under
src/YourBundle/Services/FileUploader.php
From here, my code is a bit different than what you can find on the Symfony site.
FileUploader.php
content:3) Register the service to
app/config/services.yml
:under
services
:Each argument must be in the same order as your
private
in theFileUploader.php
file. Those arguments are the ones we setted inapp/config/config.yml
underparameters
.4) Edit your controller:
The controller part is quite simple.
Add
use Symfony\Component\HttpFoundation\File\File;
in the import sectionUnder
newAction
Under
editAction
I haven't gone more far, so I can only explain what to modify after... In your
editAction
, you will also have to check that $_FILES isn't empty. If it's not, then you do the upload process. If it's, then make sure to not edit thepicture
column in the SQL query (you will have to do a custom query)5) Your twig views:
Under
show.html.twig
Change
to
Same goes for the
index.html.twig
. And you can add (not replace) it to theedit.html.twig
to get a preview of the actual picture.6) Explanations:
In
app/config/config.yml
we added a few directory to use as parameters in your files.It will later make it easier to change those directories if needed. (Won't have to edit tons of files... YAY!)
Twig directories always start from the
/web
folder.Those directory are used when we register our service as
arguments
. They will set our variable in the service fileFileUploader.php
.Unlike the Symfony site exemple, we pass the whole object to the upload service. We then, check from which class this object was created and do our upload process based in it.
Your upload process in the controller is then shortened to a single line.
In twig, we will also use the directory variable set in
app/config/config.yml
undet thetwig
property. Like said above, if our upload directory change, we will then just have to edit theapp/config/config.yml
file.I hope this will help you solve your upload issues.
Cordially,
Preciel.
Guys if you want to upload any kind of file in Symfony then I have very simple solution, which I have mentioned in the below. Why I am giving simple solutions because whenever new version come, you have to do some settings in services.yaml or you have to create extra files apart from your main controller.
So solutions is: Just use move($storing_place, $actual_filename) function in your main controller.
Put below codes in your controller file.
Hope given solution will help in your project.