0

I am trying to upload image using zend form class and my current input filter is this

$inputFilter->add([
        "name"                                           => "ImageLogo",
        "required"                                       => false,
        "validators"                                     => [
            [
                "name"                                   => "FileMimeType",
                "options"                                => [
                    "mimeType"                           => [ "image/jpeg", "image/png" ],
                ],
            ],
            [
                "name"                                   => "FileIsImage",
            ],
        ],
        "filters"                                        => [
            [
                "name"                                   => "FileRenameUpload",
                "options"                                => [
                    "target"                             => getcwd() . "/public/uploads/logos",
                    "useUploadName"                      => true,
                    "useUploadExtension"                 => true,
                    "overwrite"                          => true,
                    "randomize"                          => true,
                ],
            ],
        ],
    ]);

It uploads perfectly, but I have some parts that I couldn't able to modify.

First, I would like $entity's Id to be prepended before uploading. Something like, $entity->getId() . $fileName (Confusion: what if I'm adding a new entity. There won't be id)

Second, how would I get file name in controller after validating form. This

if ($form->isValid()) {
 print_r($form->getData());exit();
}

Shows old name.

Ali Rasheed
  • 2,765
  • 2
  • 18
  • 31

1 Answers1

0

I have a similar application. Here is how I did it if it helps.

  • I'm not prepending before uploading because as you mentioned the entity id remains uncertain until creation is complete.
  • I'm uploading first and get the image name on server.
  • I create the record in db with the tmp_name.

    // If form is submitted
    if ($this->getRequest()->isPost()) {
    
        // Merge form data and file data
        $data = array_merge_recursive(
            $this->getRequest()->getPost()->toArray(),
            $this->getRequest()->getFiles()->toArray()
        );
    
        // Filter and verify form data
        $form->setData($data);
    
        // If form is valid
        if ($form->isValid()) {
            $data = $form->getData();
            $fileNameOnServer = basename($data['ImageLogo']['tmp_name']);
    
            // entity repository manages saving into database and return new id
            $newEntityId = $this->_entityRepository->create($data);
    
            // Here we know the entity ID and the file name on server
        } else {
            // Do something if form is not valid
        }
    }
    
    // Do something if form is not submitted (e.g. display form)
    

Now I have the entity id and the file name. I don't change the file name in DB (the app is happy with the server random name) but from here all the informations are available if you need.

l_r
  • 1,060
  • 12
  • 23