XUpload - Checking full file name not just extension

Message
Author
scottwhittaker
Posts: 4
Joined: Mar 04, 2009 4:29 am

Checking full file name not just extension

#1 Postby scottwhittaker » Mar 04, 2009 4:34 am

Hi
I need to check the name and extension of the file being uploaded rather than just the extension. I want the file to be named notices.swf and I think I need to change the following to achieve this but I have no idea where to start with Javascript!! Can someone please help!

Code: Select all

function checkExt(obj)
{
    value = obj.value;
    if(value=="")return false;
    var re1 = new RegExp("^.+\.("+ext_allowed+")$","i");
    var re2 = new RegExp("^.+\.("+ext_not_allowed+")$","i");
    if( (ext_allowed && !re1.test(value)) || (ext_not_allowed && re2.test(value)) )
    {
        str='';
        if(ext_allowed)str+="\nThe file must be of the following type: "+ext_allowed.replace(/\|/g,',');
	if(ext_not_allowed)str+="\nThese extensions are not allowed:"+ext_not_allowed.replace(/\|/g,',');
        alert("Extension not allowed for file: \"" + value + '"'+str);
        return false;
    }

    return true;
}
Many many thanks for reading this!
Scott

User avatar
PilgrimX182
Posts: 2186
Joined: Mar 22, 2006 1:39 pm

#2 Postby PilgrimX182 » Mar 04, 2009 5:03 am

If you need to allow 'notices.swf' ONLY, then use this version:

Code: Select all

function checkExt(obj) 
{
value = obj.value;
if(value!="notices.swf"){alert('Please choose notices.swf file');return false;}
return true;
}

scottwhittaker
Posts: 4
Joined: Mar 04, 2009 4:29 am

#3 Postby scottwhittaker » Mar 04, 2009 5:37 am

Thank you, thats great!

Is it as easy to rename a file? Say they had "anything.swf" or just "notices" (without an extension which is possible in what I am doing) is it difficult to name the file notices.swf regardless of what the file is called to start with?

I'd say its more likely they'll upload a file called 'notices' without an extension on it.

Scott

User avatar
PilgrimX182
Posts: 2186
Joined: Mar 22, 2006 1:39 pm

#4 Postby PilgrimX182 » Mar 24, 2009 1:49 am

Update: some browsers give full path with filename instead of filename only, so here goes fix for this:

Code: Select all

function checkExt(obj) 
{ 
value = obj.value; 
if(!value.match(/notices\.swf$/){alert('Please choose notices.swf file');return false;} 
return true; 
}

scottwhittaker
Posts: 4
Joined: Mar 04, 2009 4:29 am

#5 Postby scottwhittaker » Mar 24, 2009 2:07 am

I tried that code and got a syntax error but I think it was a missing 2nd "close bracket" just before "{alert"...

Code: Select all

function checkExt(obj)
{
value = obj.value;
if(!value.match(/notices\.swf$/)){alert('Please choose notices.swf file');return false;}
return true;
}
Works from my PC so will give it a go elsewhere, very soon!

Many thanks!