Get valid image header


/ Published in: PHP
Save to your folder(s)

Get the smallest header possible from a pic, so we can add PHP code after it, and the file is still considerated as a picture


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. $imagefile = 'test.png';
  4. $output = 'test.php';
  5. $x = 8; //Nb of bytes we will read
  6.  
  7. $handle = fopen($imagefile, 'rb');
  8. $contents = '';
  9.  
  10. $i=0;
  11. while (!feof($handle)) {
  12. $contents .= fread($handle, $x);
  13. $i++;
  14.  
  15. file_put_contents($output, $contents . '<?php phpinfo(); ?>');
  16. $tabInfo = getimagesize($output);
  17.  
  18. if (in_array($tabInfo[2], array(1,2,3,6), true)) //$tabInfo['mime'] (give mimetype)
  19. {
  20. fclose($handle);
  21. die('Type detected : <b>'. image_type_to_mime_type($tabInfo[2]) .'</b>, nb Bytes : ' . $i * $x);
  22. }
  23. }
  24.  
  25. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.