Datei in Hex umwandeln.

Morgen,
also ich habe folgendes problem. Ich möchte eine Datei(z.B. ein bild) mit php via fopen einlesen und in hex umwandeln mit
Code:
function string2hex($str)
{
  if (trim($str)!="")
  {
    $hex="";
    $length=strlen($str);
    for ($i=0; $i<$length; $i++)
    {
      if ($i>0) $bound="-"; else $bound="";
      $hex.=$bound.str_pad(dechex(ord($str[$i])), 2, 0, STR_PAD_LEFT);
    }
    return $hex;
  }
}
Wie kann ich eine ganze Datei(z.B. ein bild) mit dieser Funktion in Hex umwandeln?

Danke für eure Antworten
 
Wo ist das Problem?

Code:
$infile = "deinbild.jpg";

function string2hex($str)
{
  if (trim($str)!="")
    {
      $hex="";
      $length=strlen($str);
      for ($i=0; $i<$length; $i++)
        {
          if ($i>0) $bound="-"; else $bound="";
          $hex.=$bound.str_pad(dechex(ord($str[$i])), 2, 0, STR_PAD_LEFT);
        }
      return $hex;
    }
}

if(file_exists($infile)) {
  $handle = fopen($infile, "r");
  $content = fread($handle, filesize($infile));
  fclose($handle);
}
$hexval = string2hex($content);
print "$hexval";

Wobei ich mich frage, warum du nicht einfach bin2hex() nimmst.

Code:
$infile = "deinbild.jpg";

if(file_exists($infile)) {
  $handle = fopen($infile, "r");
  $content = fread($handle, filesize($infile));
  fclose($handle);
}
$hexval = bin2hex($content);
print "$hexval";
 
Zurück
Oben