0

I am uploading the files to the folder by using file_save_upload and file_move to achieve this. Its working fine but a problem arises while saving in database. I have used FILE_EXISTS_RENAME argument, so in file_managed table filename column and uri column file name is not same. In filename column its not appending the integer value, but in uri column its appending the integer value since the file is already exist. Below is my code:

  for ($i = 0; $i < $num_files; $i++) {
      $file = file_save_upload($i, $validators, FILE_EXISTS_RENAME);   
      if ($file) { 
        $file->status = FILE_STATUS_PERMANENT;
        if ($file = file_move($file, 'public://feed-files/' . $foldername, FILE_EXISTS_RENAME)) { 
          $form_state['values']['file'][$i] = $file;
        }
        else {
          form_set_error('file', t('Failed to write the uploaded file to the selected folder.'));
          return FALSE;
        }
      }
      else {
        form_set_error('file', t('No file was uploaded.'));
        return FALSE;
      }
    }

below is the database values:

enter image description here

So how can achieve that filename and uri column file name should be same. Any suggestions will be appreciated.

  • Any particular reason of why you want both to be same? – Vivek Srivastava Sep 1 '16 at 11:02
0

The reason for difference is that multiple files with same name can be uploaded in Drupal, so when you upload a file, it adds a counter at the end of filename so that if a file with same name is uploaded again, then it doesn't replace another one.

Also the filename column contains the exact name of the uploaded file as it is used while downloading the file (the file will be downloaded with this name).

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.