Filename prefix validation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
skipZero
Forum Newbie
Posts: 3
Joined: Tue Oct 28, 2008 5:35 pm

Filename prefix validation

Post by skipZero »

Hello all,

I'm using scott's phUploader (available @ http://www.phphq.net). Im not a programmer/developer by any means, just a web designer... Anyway, the point, I need to build in an array of prefixes and have the form check against these for uploading. I've got about 28 prefixes that will go into the array. I can handle that part, what I dont know how to do is get the form to validate against this array.

The form is downloadable from the site above, so I wont bother with all the code. Or, if you can just point me to a way to validate a four character prefix followed by a '-'. Much appreciated!!
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: Filename prefix validation

Post by yacahuma »

This is the mod you require,I think. I did not tested it

Code: Select all

 
        // No errors so lets do some uploading!
        
        For($i=0; $i <= $file_uploads-1; $i++) {
                
            If($_FILES['file']['name'][$i]) {
                
       $filename = basename($_FILES['file']['name']);
       $ext = substr($filename, strrpos($filename, '.') + 1);
       if (in_array($ext, array('jpg','txt')) &&        
           @move_uploaded_file($_FILES['file']['tmp_name'][$i],$folder.$file_name[$i])) {
                    $uploaded=true;
                } Else {
                    $display_message.="Couldn't copy ".$file_name[$i]." to server, please make sure ".$folder." is chmod 777 and the path is correct.\n";
                }
            }
                
        } //For
        
    } // Else
 
 
 
put all the extensions that you want to allow in inside the in_array
skipZero
Forum Newbie
Posts: 3
Joined: Tue Oct 28, 2008 5:35 pm

Re: Filename prefix validation

Post by skipZero »

Theres already a function for testing the extension. I want the user to add predefined prefixes. In this case I'd like them to only upload PDFs which I've set in the phuploader, but the name structure should be something like pyip-theirfilename.pdf. I'd like to make sure the prefix (in this case 'pyip-') is one I've defined. How would I go about setting a function to test the PREfix?

Heres the array I've setup. how would I test the filenames first 5 characters against this array?

Code: Select all

$prefix_check=array("onbc","piyp","ibiz","wbcr","smbc","ccbo","ibfc","aebc","rbnc","icco","epsc","nplb","rmbc","hmfc","pcbu","tcbu","fbbc","gcbc",
"gbbc","tbbc","bvac","thdc","u10k","sesc","nplc","icac","cpbc","nagc","ONBC","PIYP","IBIZ","WBCR","SMBC","CCBO","IBFC","AEBC","RBNC","ICCO","EPSC",
"NPLB","RMBC","HMFC","PCBU","TCBU","FBBC","GCBC","GBBC","TBBC","BVAC","THDC","U10K","SESC","NPLC","ICAC","CPBC","NAGC");

Thanks for your help, though!
skipZero
Forum Newbie
Posts: 3
Joined: Tue Oct 28, 2008 5:35 pm

Re: Filename prefix validation

Post by skipZero »

Ok, how about this, I've got how to get an extension to validate against allowed extension types.

Code: Select all

function get_ext($key) { 
    $key=strtolower(substr(strrchr($key, "."), 1));
    // Cause there the same right?
    $key=str_replace("jpeg","jpg",$key);
    return $key;
}
$ext_count=count($allow_types);
$i=0;
How would I make this get my prefix? I just need to get the first five characters with the '-' being the fifth. so something near this?

Code: Select all

function get_pref($prekey) { 
    $prekey=strtolower(substr(strrchr($prekey, "-"), -4));
    return $prekey;
}
As I've little idea of how this works, Im figuring the line starting with $prekey= is saying change all to lowers, substring find charatcer - and start minus 4 characters back. Am I remotely close!? Is there a way to say characters from start up to '-' = $prekey and to compare that to the array such as this

Code: Select all

If(!in_array($pref, $prefix_check)) {
                            
                $error.= "Invalid prefix for your file: ".$_FILES['file']['name'][$i].", only ".$types." are allowed.<br />Your file(s) were <b>not</b> uploaded.<br />";
                }
Im hoping Im in the ballpark, at least!! Any help GREATLY appreciated!!
Post Reply