0

Form function:

$form['upload_photo'] = array(
        '#type' => 'file',
        '#title' => t('Upload Your Photo'),
        '#title_display' => 'before',
        '#states' => array(
            'visible' => array(
                ':input[name="testimonial_type"]' => array('value' => 'Text'),
            ),
        ),
        '#prefix' => '<tr><td>',
        '#suffix' => '</td></tr>',
    );

Submit function:

$field_collection_item = entity_create('field_collection_item', array('field_name' => 'field_text_testimonial_info'));
$file_d = $form_state['values']['upload_photo'];
                $file = file_load($file_d);
                $file->status = FILE_STATUS_PERMANENT;
                file_save($file);
                file_usage_add($file, 'user', 'TYPE', $file->fid);
                $file_info = image_get_info($file->uri);
                $file_arr = array(
                'fid' => $file->fid,
                );
                $field_collection_item->field_client_image[LANGUAGE_NONE][0] = $file_arr;
                $field_collection_item->setHostEntity('user', $user);
                $field_collection_item->save();

After submit I am getting below error:

  • Notice: Undefined property: stdClass::$uri in file_save() (line 614 of /public_html/includes/file.inc).
  • Notice: Undefined property: stdClass::$filename in views_data_export_file_presave() (line 278 of /public_html/sites/all/modules/contrib/views_data_export/views_data_export.module).
  • PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'uri': INSERT INTO {file_managed} (filesize, status, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => 1 [:db_insert_placeholder_2] => 1535026403 ) in drupal_write_record() (line 7386 of /public_html/includes/common.inc).

Kindly help me to solve it out

1 Answer 1

0

Maybe this can help How to upload file in drupal8 forms with form element type file?

I see for your use case it makes more sense to use Managed file type.

A couple of observations, something you should look at,

  • It seems you are getting SQL errors due to trying to upload a file which is already present in the table, you should check why this happens and perhaps programmatically change the name of the file if it already exists. See file_save_data
  • Errors from views, not sure where you are trying to submit this form, but it is clearly breaking something in views_data_export or it maybe because of the next point.
  • Lastly, check the link above for how to properly handle file uploads in form, it looks like file_save($file); is breaking for you due to missing uri

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.