【问题标题】:Symfony4: Image cannot be savedSymfony4:图像无法保存
【发布时间】:2021-05-28 10:22:39
【问题描述】:

我已经从 Symfony 3.4 更新到 4.0 并验证了操作。 图像保存功能显示“已完成”,但图像未保存。 因为保存目的地设置为 web/uploads, 在Image.php中,我把图片目录路径从web改成了public,但是没变。 还有什么我需要改变的吗?

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

图片.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';
    }

后记

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>

【问题讨论】:

  • 也许这是 Symfony 的事情,但实际上传文件的代码在哪里?您是否也更改了那里的目标目录?
  • @AkenRoberts 我正在使用 uploadifive,但似乎保存目的地取决于 Symfony 设置。
  • 你能检查你的树枝模板/enctype="application/x-www-form-urlencoded" 设置的渲染 html 吗?
  • @dbrumann 没有。没有 enctype 设置。以防万一,我添加了树枝文件,所以请检查一下。
  • 为了让 FileUpload 工作,您必须在表单上设置 enctype,例如通过将其添加到{'attr': { 'enctype': 'multipart/form-data'}}(我认为)。你可以在 SymfonyCasts 的这个免费视频中看到它:symfonycasts.com/screencast/symfony-uploads/…

标签: php symfony


【解决方案1】:

你应该使用projectDir 而不是你的getUploadRootDir() 和危险的../../

查看这篇帖子Symfony 4, get the root path of the project from a custom class (not a controller class)了解详细说明

Symfony 3.3 中的新功能:https://symfony.com/blog/new-in-symfony-3-3-a-simpler-way-to-get-the-project-root-directory

【讨论】:

    猜你喜欢
    • 2016-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-05
    • 1970-01-01
    • 2017-04-24
    • 2018-11-20
    相关资源
    最近更新 更多