2

I have updated from Symfony 3.4 to 4.0 and have verified the operation.   The image save function says "Completed", but the image is not saved.   Because the save destination was set to web/uploads, In Image.php, I changed the image directory path from web to public, but it didn't change.   Is there anything else I need to change?  

ImageController.php

    public function saveAction(Request $request)
    {
        $response = new Response();
        $imageService = $this->get("admin.imageService");
        if ($token === $imageService->getImageToken($staffId, $uniq)) {
            try {
                $imageService->saveImage($image);
                // Normal termination parameters #This is required for onComplete to fire on Mac OSX
                $msg = $this->container->getParameter('uploadify_success');
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                $response->setStatusCode(400);
            }
        } else {
            $msg = 'Access is illegal.';
            $response->setStatusCode(400);
        }
        $response->send();

ImageService.php

    public function saveImage(Image $image)
    {

        // Set the extension (already set when saving from an email attachment)
        $ext = $image->getImageExtension();
        if (!$ext && $image->getFileUpload()) {
            $ext = $image->getFileUpload()->guessExtension();
            $image->setImageExtension($ext);
        }

        // save
        $this->entityManager->persist($image);
        $this->entityManager->flush();
    }

Image.php

    private function getUploadRootDir()
    {
        // Absolute path to the location to save the uploaded file
        return __DIR__.'/../../../../../../public/'.$this->getUploadDir();
    }

    /**
     * Returns the image directory name
     *
     * @return string
     */
    private function getUploadDir()
    {
        return 'uploads';
    }

Postscript

index.html.twig

{% block javascripts %}
    {{ parent() }}
    <script src="{{ asset('uploadifive/jquery.uploadifive.js') }}"></script>
    <script src="{{ asset('js/image.five.js') }}"></script>
{% endblock %}

{% block stylesheets %}
    {{ parent() }}
    <link rel="stylesheet" href="{{ asset('uploadifive/uploadifive.css') }}">
{% endblock %}

{# contentBody #}
{% block contentBody %}
    {{ render(controller('AppBundle:Hq/Image:manager')) }}
{% endblock %}

manager.html.twig

        <div class="tabcontent">
            <div class="operations">
                {{ form_start(form, {"attr": {"id": "uploadForm"}}) }}
                    {{ form_widget(form.fileUpload) }}
                    {{ form_rest(form) }}
                {{ form_end(form) }}
                &nbsp;
            </div>
            <div id="fileQueue"></div>
        </div>
benq_0511
  • 21
  • 3
  • Maybe this is a Symfony thing, but where is the code that actually uploads the file? Did you change the destination directory there, too? – Aken Roberts Feb 26 '21 at 02:04
  • @AkenRoberts I'm using uploadifive, but it seems that the save destination depends on the Symfony settings. – benq_0511 Feb 26 '21 at 08:10
  • Can you check in your twig-template/the rendered html that `enctype="application/x-www-form-urlencoded"` is set? – dbrumann Feb 26 '21 at 08:14
  • @dbrumann No. There was no enctype setting. Just in case, I added the twig file, so please check it. – benq_0511 Feb 26 '21 at 09:52
  • In order for FileUpload to work you must set the enctype on your form, e.g. by adding it to `{'attr': { 'enctype': 'multipart/form-data'}}` (I think). You can see it in this free video from SymfonyCasts: https://symfonycasts.com/screencast/symfony-uploads/upload-request#multipart-form-data – dbrumann Feb 26 '21 at 20:16

1 Answers1

0

You should use projectDir istead of your getUploadRootDir() with hazardous ../../

Take a look at this post Symfony 4, get the root path of the project from a custom class (not a controller class) for the detailed instructions

New in Symfony 3.3 : https://symfony.com/blog/new-in-symfony-3-3-a-simpler-way-to-get-the-project-root-directory

ThEBiShOp
  • 506
  • 8
  • 23