0
/**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->file) {
            $this->path = $this->file->guessExtension();
        }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->file) {
            return;
        }

        // you must throw an exception here if the file cannot be moved
        // so that the entity is not persisted to the database
        // which the UploadedFile move() method does
        $this->file->move($this->getUploadRootDir(), $this->id.'.'.$this->file->guessExtension());

        unset($this->file);
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAbsolutePath()) {
            unlink($file);
        }
    }

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

hi in this cose i upload the file when i saving the filename id into directory all work, but into database saving jpg only...without id

4
  • 1
    10 questions and no accepted answers... Please go back to your previous questions, and mark the answer that solved your issue as accepted by clicking the 'tick' icon underneath the answer's votes. Commented Feb 8, 2012 at 16:54
  • aaaa ok i not know this method ok wait Commented Feb 8, 2012 at 16:59
  • I don't understand the question: what does not work? Do you see anything in the database? If yes, what? Commented Feb 8, 2012 at 17:43
  • in database id name path 1 utkukur jpg Commented Feb 8, 2012 at 17:48

3 Answers 3

2

In the pre persist lifecycle call back your entity has not yet been assigned an ID therefore $this->id.'.'.$this->file->guessExtension() when calling this, $this->id will be null

You have followed the example in the cookbook correctly. It just fails to mention that the only thing stored in the database under path will be the extension. This doesn't matter as the filename will always be the id concatenated with the extension. i.e. $filename = $this->id . $this->path

You will therefore have to think of a different naming strategy if you want the string stored under the path to exactly represent the filename.

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

Comments

0

In your preUpload method, you just affect the extension file to the path property. So, it's normal in your database you just have the jpg extension.

EDIT: In your preUpload method, the ID is not yet affected by the ORM, so you can't use it to generate the file name. IMO, you should use uniquid to generate a unique file name. Something like $this->path = uniquid().'.'.$this->file->guessExtension()

1 Comment

if i add $this->path = $this->id.'.'.$this->file->guessExtension(); always save only jpg, without id
0

Okay i had the same problem once(only with .txt).

I think it might be a problem if you have special charakters in your filename.

i solved it this way:

            $filepath = $this->file->getClientOriginalName();
        $filepath = str_replace(array('/',  ' ', '-', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß'), array('_', '_', '', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss'), $filepath);
        $this->path = $filepath;

hope this is the answer for your problem, if it isn't solved yet anyways :D

Comments

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.