Posted by carlovdb on November 6, 2012 at 1:33pm
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.
<?php
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:
<?phpif ($file = file_move($file, 'public://', FILE_EXISTS_REPLACE)) {
?>
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 :)
<?phpfunction form_example_tutorial_10_validate($form, &$form_state) {
$file = file_save_upload('file', array(
'file_validate_extensions' => array('csv'), // Validate extensions.
'public://',FILE_EXISTS_REPLACE
));
// If the file passed validation:
if ($file) {
// Move the file, into the Drupal file system
if ($file = file_move($file, 'public://', FILE_EXISTS_REPLACE)) {
// 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.'));
}
}
?>