2

I'm writing a really simple test site, I have images in one of my project's folder, and I simply display them on the site. I don't have any entities or database interactions so no form either.

Now, I would like to upload a file in the most simple way. :

Choose a file -> move it in my project's images folder.

I had a look on the doctrine tutorial but it's for entity images.

Is it possible ? Many thanks.

1

1 Answer 1

6

Don't conflate entities with forms. Symfony forms work fine with just arrays.

The suggested link in the comments is something I posted a few years ago. But it is a bit confusing.

In any event, here is the simplest way I know to upload a file. No Symfony forms or Doctrine entities required.

Lets assume you have generated an html form that looks like:

<form action="/upload" method="post" enctype="multipart/form-data">
  <input type="file" name="file" required>
  <input type="submit" value="Upload">
</form>

So the user selects a file and pushed the button. Your upload controller action kicks off. And if you don't know how to create either the form html or link it to a controller action then you really need to spend more time on the basics.

public function uploadAction(Request $request)
{
    // See if posted
    if ($request->isMethod('POST')) {

        // Pull the uploaded file information
        /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */
        $file = $request->files->get('file');

        // And now you have access to assorted file info such as
        $filePath = $file->getRealPath();
Sign up to request clarification or add additional context in comments.

2 Comments

Quick note: shouldn't it be if ($request->isMethod('POST'))... ?
@RomainBruckert Wow. Good eye. Maybe I left that as an exercise? In any event. It is fixed.

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.