// Rename and update the file record accordingly.
if (file_move($file->filepath, $destination, FILE_EXISTS_RENAME)) {
db_query("UPDATE {files} SET filepath = '%s', filename = '%s' WHERE fid = %d", $file->filepath, $filename_conv, $file->fid);
$count++;
}
else {
$errors[] = $file->filepath;
}
}
if ($errors) {
$message = t('Not all file names could be converted. The following files could not be accessed and have been ignored:');
$message .= theme('item_list', $errors);
drupal_set_message($message, 'error');
}
else {
drupal_set_message(t('@filenames have been successfully transliterated.', array('@filenames' => format_plural($count, '1 file name', '@count file names'))));
}
I came across this issue.. it is to do with characters corrupting file names...
I had 11 files that I couldn't transliterate because PHP couldn't find the file. That's because the file name was garbled on my Mac OS X system after pulling them down from some kind of *nix system.
I believe the only reason the above code is needed is due to this line:
I don't understand what Transliteration has to do with moving files between filesystems. Can you be more precise in specifying what exactly is the bug in this module?
Comments
Comment #1
mibfire commentedi solved myself:
/* transliteration module own implement */
function bandg_transliteration_clean_filename($filename, $source_langcode = NULL) {
$filename = transliteration_get($filename, '', $source_langcode);
// Replace whitespace.
$filename = str_replace(' ', '-', $filename);
// Remove remaining unsafe characters.
$filename = preg_replace('![^0-9A-Za-z_.-]!', '', $filename);
// Force lowercase to prevent issues on case-insensitive file systems.
if (variable_get('transliteration_file_lowercase', TRUE)) {
$filename = strtolower($filename);
}
return $filename;
}
/* i call this function from own node-form(video) submit function */
function bngi_transliteration_retroactive() {
module_load_include('inc', 'transliteration', 'transliteration.admin');
$count = 0;
$errors = array();
$result = db_query(transliteration_file_query());
while ($file = db_fetch_object($result)) {
if (!file_exists('./'. $file->filepath)) {
// Missing file.
$errors[] = $file->filepath;
continue;
}
// Sanitize file name.
$filename_org = basename($file->filepath);
$filename = transliteration_clean_filename($filename_org);
// Build destination path.
$destination = dirname($file->filepath) . '/' . $filename;
$filename_conv = bandg_transliteration_clean_filename($filename_org);
// Rename and update the file record accordingly.
if (file_move($file->filepath, $destination, FILE_EXISTS_RENAME)) {
db_query("UPDATE {files} SET filepath = '%s', filename = '%s' WHERE fid = %d", $file->filepath, $filename_conv, $file->fid);
$count++;
}
else {
$errors[] = $file->filepath;
}
}
if ($errors) {
$message = t('Not all file names could be converted. The following files could not be accessed and have been ignored:');
$message .= theme('item_list', $errors);
drupal_set_message($message, 'error');
}
else {
drupal_set_message(t('@filenames have been successfully transliterated.', array('@filenames' => format_plural($count, '1 file name', '@count file names'))));
}
// Flush page cache.
cache_clear_all();
}
Comment #2
hypertext200If you can provide a patch then it would be better.
Comment #3
Jorrit commentedWhat problem do you experience when filenames contain spaces?
Comment #4
Jorrit commentedClosed because of lack of response.
Comment #5
dynamicdan commentedI came across this issue.. it is to do with characters corrupting file names...
I had 11 files that I couldn't transliterate because PHP couldn't find the file. That's because the file name was garbled on my Mac OS X system after pulling them down from some kind of *nix system.
I believe the only reason the above code is needed is due to this line:
It would remove any unknown characters before saving/uploading a file and thus avoid any corrupted file naming problems.
This information should be documented in the transliteration module. Not here. Updating issue.
Comment #6
dynamicdan commentedFYI, the UI for this module makes it look like everything should work. Perhaps this is a bug?
Comment #7
amateescu commentedI don't understand what Transliteration has to do with moving files between filesystems. Can you be more precise in specifying what exactly is the bug in this module?
Comment #8
amateescu commentedClosing until we get further debugging info here, or what actually needs to be documented.
Comment #9
dynamicdan commented@amateescu
File names look different on different file systems (and in diff FTP/terminal apps).
You need the file names to test I guess... don't really have them anymore though :(
Will try and post more info when I get it.