Using phpvalidation to control the file name in the plugin fileupload

GorYa

Member
Faced with the need to check the uploaded file name and its variations in the event of incorrect names. In my case, many do not read the rules and write the name in the Cyrillic alphabet, and it is necessary to write in Latin. How to solve it you have two options, but for some reason they do not work, maybe I'm doing something wrong, because my knowledge of php is not very large.
The first code to validate that if there are Russian letters in the name will generate an error:
PHP:
if(!preg_match('#[^a-z0-9_\-\. ]#ui', $name)){
    if(strlen($name) == 0){//Checking the name are blank
        $error[] = 'Fill out the field <b>Files</b>';
    }
    else{
        $error[] = 'The file name is used Cyrillic';
    }
}

The second option is to do a transliteration (option "Random name" does not fit, as needed source name):
PHP:
unction remove_shit($name) {
    static $table = array(
        '?' => 'a',
        '?' => 'b',
        '?' => 'v',
        ...
        '?' => 'A',
        '?' => 'B',
        '?' => 'V',
        ...
    );
    return preg_replace('#[^-._a-z0-9]#ui', '', strtr($name, $table));
}

I would be very grateful for your help, because he could not figure out what kind of settings in the plugin phpvalidations need to put these techniques to work?
 
Hmmm. Unfortunately I don't think the php replace validation will work for this, simply because of the way uploads are handled, and where the filename is stored (it's not actually in the element data, it's in the special PHP $_FILES[] array).

There is a recent thread discussing how to change filenames with a form plugin. I'll see if I can dig that up.

-- hugh
 
Optimally, for example, the use of transliteration. It probably is possible to use only in special cases, but perhaps in the future it will be possible to unify.
We used the russian standard is effective for transliteration ISO 9:1995
In file filesystemstorage.php rows 188-195 add function (in my case, to translate the cyrillic alphabet in latin):
PHP:
function translitText($str)
{
    $tr = array(
        "?"=>"A","?"=>"B","?"=>"V","?"=>"G",
        "?"=>"D","?"=>"E","?"=>"E","?"=>"ZH","?"=>"Z","?"=>"I",
        "?"=>"Y","?"=>"K","?"=>"L","?"=>"M","?"=>"N",
        "?"=>"O","?"=>"P","?"=>"R","?"=>"S","?"=>"T",
        "?"=>"U","?"=>"F","?"=>"H","?"=>"CZ","?"=>"CH",
        "?"=>"SH","?"=>"SHH","?"=>"``","?"=>"Y`","?"=>"`",
        "?"=>"E`","?"=>"YU","?"=>"YA","?"=>"a","?"=>"b",
        "?"=>"v","?"=>"g","?"=>"d","?"=>"e","?"=>"e","?"=>"zh",
        "?"=>"z","?"=>"i","?"=>"y","?"=>"k","?"=>"l",
        "?"=>"m","?"=>"n","?"=>"o","?"=>"p","?"=>"r",
        "?"=>"s","?"=>"t","?"=>"u","?"=>"f","?"=>"h",
        "?"=>"cz","?"=>"ch","?"=>"sh","?"=>"shh","?"=>"``",
        "?"=>"y`","?"=>"`","?"=>"e`","?"=>"yu","?"=>"ya"
    );
    return strtr($str,$tr);
}
if (preg_match('#[^a-z0-9_\-\.]#u', '_', $filename) {
   $filename = translitText($filename);
   $filename = preg_replace('#[^a-z0-9_\-\.]#u', '_', $filename);
    $this->randomizeName($filename);

     return $filename;
}

If successful, the method can be applied to other languages.
You may need to correct something in other files.
But this is only my thoughts, and since I'm not good at php, the code may not be correct.
 
Last edited:
Trasliterating is a pain, because it's so language dependent, and althhouh iconv() can handle a lot of languages, it doesn't handle Cyrilic, so (as you've found) you need to do it by hand.

It's on my list of things to work on, but until then, you'll have to run some customizec code.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top