1

I have created a file uploads page. In my controller I want to get the uploaded path of the view and add it in the database for a particular id. For that I want the path of the file ans send it to the repository. The problem when I am using in my controller

if ($request->getMethod() == 'POST')

      { 
                $form->bind($request);

        $file = $form["file"]->getData();

                /* here it is giving the path like /tmp/phpkR4kgD */
                $em = $this->getDoctrine()->getManager();
                $user->upload();
            }
this is my entity

/**
     * Sets file.
     *
     * @param UploadedFile $file

     */

    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
    }
    public function upload()
    {
    if (null === $this->file) 
    {
            return;
        }
    $this->file->move($this->getUploadRootDir(), $this->file->getClientOriginalName());
    $this->path = $this->file->getClientOriginalName();
    $this->file = null;
    }
    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }

    public function getAbsolutePath()
    {
        return null === $this->path
            ? null
            : $this->getUploadRootDir() . DIRECTORY_SEPARATOR . $this->path;
    }

    public function getWebPath()
    {
        return null === $this->path
            ? null
            : $this->getUploadDir() . DIRECTORY_SEPARATOR . $this->path;
    }

    protected function getUploadRootDir()
    {
        return __DIR__ . '/../../../../web/'. $this->getUploadDir();
    }

    protected function getUploadDir()
    {
        return 'uploads/';
    }

I have created my uploads folder in web folder of symfony

1 Answer 1

1

while calling upload() method from it takes the temporary path to entity.

In entity it will get the orginal path $this->path = $this->file->getClientOriginalName();

so use return statement which returns the original path to controller from there you can save it in database...

Sign up to request clarification or add additional context in comments.

1 Comment

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.