Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder durangod's Avatar
    Join Date
    Nov 2010
    Location
    East Texas USA
    Posts
    2,220
    Thanks
    378
    Thanked 60 Times in 58 Posts

    validating zip file via file header

    So according to this

    https://en.wikipedia.org/wiki/List_of_file_signatures

    not quite half way down the page is zip file listing

    When i pull the first 12 bytes of the zip file i get PKx.....

    The PK seems to be the ISO 8859-1 value which is fine, i can check for that also.

    and the hex value (when i convert it) should be 50 4B 03 01


    I am not getting that hex value so i wanted to check, is that document corrrect?

    hex value im getting is 504b0304 should be 01

    im using this
    PHP Code:
    //get hex value 
                                         
    $getbytes substr($zipsubheader04);
                                         
    $hxval bin2hex($getbytes); 

    Nevermind how the heck did i get 01 in my head, its correct it should be 04 per the wiki.... somehow i copied that as 01
    Last edited by durangod; Jun 18th, 2019 at 10:15 PM.
    If a php file only has php code within it you do not need to use the closing php tag
    A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
    durangod is short for durango dave

  2. #2
    Senior Coder durangod's Avatar
    Join Date
    Nov 2010
    Location
    East Texas USA
    Posts
    2,220
    Thanks
    378
    Thanked 60 Times in 58 Posts
    here is the whole thing and working i shortened it a bit

    PHP Code:

             $ziphdr 
    fread($ziphandle4); //should grab first 4 of header
             
    fclose($ziphandle);                                      
                                        
                                     
             
    //now clean it up a bit    
             
    $zipsubheader trim($ziphdr);
                                         
             
    //get hex value 
         
    $getbytes substr($zipsubheader02);
         
    $getbytes4 substr($zipsubheader04);
         
    $hxval bin2hex($getbytes4);
                                         
              if( 
    $hexval == '504b0304' && $getbytes == "PK")
              {
                                             
                  echo 
    "<pre>";
                  echo 
    "passes as zip";
                  exit;
                                            
                }else{

                         echo 
    "<pre>";
                         echo 
    "fails as zip";
                         exit;

                        }
    //close else 
    If a php file only has php code within it you do not need to use the closing php tag
    A good way to remember objects from arrays is you shoot objects with arrows Example: $name->id; then Arrays are $name['id'];
    durangod is short for durango dave


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •