By carlovdb on
Hello,
I have created a new module based on example 10. Now, this works all fine, but I just want to overwrite the file, not renaming it with filename_[digit].extension
I thought this was possible with FILE_EXISTS_REPLACE, but this doesn't do the job..
This is the code that I use.
function form_example_tutorial_10_validate($form, &$form_state) {
$file = file_save_upload('file', array(
'file_validate_extensions' => array('csv'), // Validate extensions.
FILE_EXISTS_REPLACE
));
// If the file passed validation:
if ($file) {
// Move the file, into the Drupal file system
if ($file = file_move($file, 'public://')) {
// Save the file for use in the submit handler.
$form_state['storage']['file'] = $file;
}
else {
form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
}
}
else {
form_set_error('file', t('No file was uploaded.'));
}
}
Can someone help me?
Comments
If what I asks is not
If what I asks is not possible, is it possible to first do a check if the file exists and then delete the older file and then save the new one with the correct filename?
FILE_EXISTS_REPLACE is the
FILE_EXISTS_REPLACE is the correct value to use, you're just using it in the wrong place. Change the file_move call:
Solved and ashamed :) If you
Solved and ashamed :)
If you don't add the FILE_EXISTS_REPLACE to the file_move, this can't work :)
I tried your code and it
I tried your code and it didn't do what you claim it does.
On the line that has "$file = file_save_upload('file', array(", what is "file" !? Is it a full-fledged file name? Is it a file name without the extension? Is it a form field name?
Are we sure that this is an
Are we sure that this is an actual replace? When I replace a file that exists, I also set permanent and new file name, I get a new file ID. if you check in the managed files, both are listed (having the same file name but different file ID). It seems like this is a hack. Wouldn't we expect the file ID to never change if it is a "replace". Also within managed files, shouldn't there list only the single file? - or am i way overthinking