TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Javascript, AJAX, E4X (http://www.talkphp.com/javascript-ajax-e4x/)
-   -   checking file extensions. How can I change this? (http://www.talkphp.com/javascript-ajax-e4x/1257-checking-file-extensions-how-can-i-change.html)

Sam Granger 09-29-2007 08:07 AM

checking file extensions. How can I change this?
 
Code:

        function checkFileExtentions(form){
                if(check_file_extentions == false){ return false; }
                var re = /(\.php)|(\.sh)$/i;
                if(form['filename'].value != ""){
                        if(form['filename'].value.match(re)){
                                var string = form['filename'].value;
                                var num_of_last_slash = string.lastIndexOf("\\");
                                if(num_of_last_slash < 1){ num_of_last_slash = string.lastIndexOf("/"); }
                                var file_name = string.slice(num_of_last_slash + 1, string.length);
                                var file_extention = file_name.slice(file_name.indexOf(".")).toLowerCase();
                                alert('Sorry, uploading a file with the extention "' + file_extention + '" is not allowed.');
                                return true;
                        }
                }
                return false;
        }

I have the following js code. At the moment, it warns me if I want to upload a php file. How can I make it warn me for all file extensions, with the exception of .jpg? :confused:

mortisimus 10-01-2007 02:28 PM

Looks like it will warn you every time. I'm no good at javascript but maybe do a check for something like:

if (file ext. doesn't equal jpg){
alert("error!");
}
else
{
//carry on with upload or whatever...
}

sorry but I do not know the right js code to put in...

Salathe 10-01-2007 03:05 PM

Code:

// Returns true if the extension is invalid
function checkFileExtentions(form)
{
        if (check_file_extentions == false)
        {
                return false;
        }
       
        // Regex of valid extensions
        var re = /\.(jpg|jpeg|png|gif|bmp)$/i
       
        // If we have a filename to check and it is invalid
        if (form['filename'].value != '' && !form['filename'].value.match(re))
        {
                var name = form['filename'].value;
                var ext  = name.substr(name.lastIndexOf('.')+1).toLowerCase();
                alert('Sorry, uploading a file with the extention "' + ext + '" is not allowed.');
                return true;
        }
       
        return false;
}


Sam Granger 10-03-2007 03:31 PM

Thanks Salathe! That code of yours worked like a dream!

Salathe 10-03-2007 03:32 PM

You're most welcome, I'm here to help. :)

Geert 12-09-2007 04:51 PM

The "jpg" and "jpeg" extensions can be merged into one alternation option, which should be faster.
Code:

/\.(jpe?g|png|gif|bmp)$/i
Also, since you're not using the captured parentheses, make them non-capturing, which should be faster.
Code:

/\.(?:jpe?g|png|gif|bmp)$/i


All times are GMT. The time now is 04:35 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0