简体   繁体   中英

SYMFONY3 upload files without entity

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM