I am trying to add image upload functionality to my custom module.

Images are successfully being uploaded via the following: `

$form['slide']['dbs_img'] = array(
          '#type' => 'managed_file',
          '#title' => 'Member Slide',
          '#description' => 'Add slides associated to your listing',
          '#upload_location' => 'public://upload/business_listing/slides',
          '#required' => ($this->listing) ? FALSE : TRUE,
          '#multiple' => FALSE,
          '#attributes' => array('class' => array())

The problem arises when a image with that name already exists. The image being uploaded automatically has "_#" appended to its name (this is great), but when I try retrieve the images new name to store in my database using the following:

            // handle the dbs_img
        /* Fetch the array of the image file */
        $slide = $form_state->getValue('dbs_img');
        /* Load the object of the image file by it's fid */
        $file = File::load($slide[0]);
        /* Set the status flag permanent of the slide file object */
        // get file name
        $dbs_img = $file->getFileName();

The origional image name is returned instead of the name it has been renamed to. How do I get the name image name to store in my database? Any help and advice would be greatly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.