I am trying to implement a file upload form in which files are saved. I had written the following code
<?php
/**
* @file
* Contains \Drupal\avenue_product_import\Form\avenue_product_importForm.
*/
namespace Drupal\avenue_product_import\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
class AvenueForm extends FormBase {
public function getFormId() {
return 'AvenueForm';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['description'] = [
'#markup' => '<p>Use this form to upload a CSV file of Data</p>',
];
$form['import_csv'] = [
'#type' => 'managed_file',
'#title' => t('Upload file here'),
'#upload_location' => 'public://importcsv/',
'#default_value' => '',
"#upload_validators" => ["file_validate_extensions" => ["csv"]],
'#states' => [
'visible' => [
':input[name="File_type"]' => ['value' => t('Upload Your File')],
],
],
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Upload CSV'),
'#button_type' => 'primary',
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
/* Fetch the array of the file stored temporarily in database */
$csv_file = $form_state->getValue('import_csv');
/* Load the object of the file by it's fid */
$file = File::load($csv_file[0]);
/* Set the status flag permanent of the file object */
$file->setPermanent();
/* Save the file in database */
$file->save();
drupal_set_message('File Uploaded Successfully');
}
}
But here the problem is if the filename in which I am uploading is same. It is getting renamed. But I want to replace old with the new one. How can I achieve this? I had seen file_move()
and file_save_upload()
function. What changes should I make to replace existing filename with the newly uploaded CSV file?
FILE_EXISTS_RENAME
is the default with no way to override it yet. drupal.org/project/drupal/issues/2648816