0

I am working on a form which accepts some user input and an image file, the submission part and the data getting entered into the database is working fine but I am stuck at how to name a file once it is uploaded, right now this is what i see as an image name in database C:\wamp2.5\tmp\phpF360.tmp which obviously is not correct.

This is what my controller looks like DefaultController.php

public function createBlogAction(Request $request)
{

    $post = new Post();
    $form = $this->createForm(new PostCreate(), $post);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $post->upload();
        $post->setDate(date_create(date('Y-m-d H:i:s')));
        $post->setAuthor('ClickTeck');
        $em->persist($post);
        $em->flush();

        $this->get('session')->getFlashBag()->add(
            'notice',
            'Success'
        );
    }

    return $this->render('BlogBundle:Default:blog-create.html.twig', array(
            'form' => $form->createView()
        )
    );
}

This is what my upload() looks like inside Entity/Post.php which is uploading the file and moving it into the folder, the file name that I see in a folder is correct however now the one that goes into the database

public function upload()
{
    if (null === $this->getImage()) {
        return;
    }

    // I might be wrong, but I feel it is here that i need to name the file
    $this->getImage()->move(
        $this->getUploadRootDir(),
        $this->getImage()->getClientOriginalName()
    );

    $this->path = $this->getUploadDir();
    $this->file = null;
}

I will really appreciate if someone can push me in right direction, I just need to name the file, a name which gets assigned to the image in database and the file should get uploaded with the same name as well.


UPDATE

I managed to get it to work using the following function, not sure if this is the best practice but it did work, i would love to hear from others on this. please do not provide any links, if you can refine what has already been done that would be great.

public function upload()
{
    // the file property can be empty if the field is not required
    if (null === $this->getImage()) {
        return;
    }
    $dirpath = $this->getUploadRootDir();
    $image = $this->getImage()->getClientOriginalName();
    $ext = $this->getImage()->guessExtension();
    $name = substr($image, 0, - strlen($ext));
    $i = 1;
    while(file_exists($dirpath . '/' .  $image)) {
        $image = $name . '-' . $i .'.'. $ext;
        $i++;
    }
    $this->getImage()->move($dirpath,$image);
    $this->image = $image;
    $this->path = $this->getUploadDir();
    $this->file = null;
}
Shairyar
  • 3,268
  • 7
  • 46
  • 86
  • [function.move-uploaded-file](http://php.net/manual/en/function.move-uploaded-file.php) is normally used to transfer and rename the 'temporary' uploaded file to somewhere permanent. An example: [questions/6179178/moving-an-uploaded-file-using-php](http://stackoverflow.com/questions/6179178/moving-an-uploaded-file-using-php). – Ryan Vincent Oct 01 '14 at 10:31
  • I find your question unclear: "the submission part and the data getting entered into the database is working fine but I am stuck at how to name a file once it is uploaded" "the file name that I see in a folder is correct however now the one that goes into the database" That's rather contradictory isn't ? What's that function move() ? as pointed by Ryan Vincent, php function is not this one. – Eagle1 Oct 01 '14 at 10:37
  • @Eagle1 what i mean is i can get the image uploaded just fine and the file name and path is being inserted into database just fine as well, if is the name of file that i am having the problem with, the name that gets saved is like `:\wamp2.5\tmp\phpF360.tmp` rather than `nameofimage.jpg` – Shairyar Oct 01 '14 at 13:57
  • i am strictly following http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html, i dont think i am using parts of both – Shairyar Oct 01 '14 at 15:22

2 Answers2

1

This topic from documentation may help you : http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html

In addition, you should not put your upload function in the controller but rather use Doctrine events (Lifecycle callbacks) to call your function automatically.

  • Ya i have already gone through this documentation couple of times, eventually the controller will be as thin as possible but since i am in my learning mode i just wanted to get it to work and then put everything where it should be – Shairyar Oct 01 '14 at 13:53
  • just so it is clear the upload function is not inside the controller it is inside the `Entity/Post.php` file which is making use of Doctrine – Shairyar Oct 01 '14 at 14:04
0

as per suggestion of @theofabry you can check symfony2 documentation How to handle File Uploads with Doctrine, Controller must be thin as much as possible and try to do upload with Doctrine Events.

If you want to continue with your logic you may try following code, I have not tested yet...so please be careful.

   // set the path property to the filename where you'ved saved the file
   $this->path = $this->file->getClientOriginalName();

instead of

 $this->path = $this->getUploadDir();
kuldipem
  • 1,719
  • 1
  • 19
  • 32
  • ya i know and i agree with you about the controller, this is not how the controller is going look eventually but just for the sake of getting the document named correctly i tried what you mentioned, both of them work find for the image path but not the image name – Shairyar Oct 01 '14 at 13:55
  • just so it is clear the upload function is not inside the controller it is inside the `Entity/Post.php` file which is making use of Doctrine – Shairyar Oct 01 '14 at 14:05