How to fix special character file names issues during file uploads from custom and drupals form. specially for security issue.
In hook_init() you can place code for altering $_FILES variables name values to store proper and good file names.
<?php
/**
* Last mod : Inder SIngh @ Sat 12 Mart 2:18 AM 2010
*/
function inders_mod_init() {
//Add any css or js file there
drupal_add_css(drupal_get_path('module', 'inders_mod') .'/inders_mod.css');
//If custom field were used for uploading
//Never prefered
if(!empty($_FILES['avatar_image']) || !empty($_FILES['profile_pic'])){
foreach ($_FILES as $field_name => $field) {
$_FILES[$field_name]['orig_name']=$field['name'];
$_FILES[$field_name]['name']=preg_replace('`[^\.a-z0-9]`i','_',$field['name']);
}
}
//This will work for all files if using drupal forms
if (!empty($_FILES['files'])){//Working with global
foreach ($_FILES['files']['name'] as $field => $filename) {
if($field=="diagram"){
$_FILES['files']['orig_name'][$field] = $filename;//May be useful for some cases
//Get the trick to work here
$_FILES['files']['name'][$field] = preg_replace('`[^\.a-z0-9]`i','_',$filename);
}
}
}
}
/**
*
*/
?>