0
此代碼工作正常(但我擔心它會失敗,大輸入文件)。示例1讀取整個文件並且不循環,示例2讀取3k塊並循環直到EOF。base64_decode在循環中運行時創建損壞的圖像
$in = fopen("in.b64", 'r');
$out = fopen("out.png", 'wb');
if ((!$in) || (!$out)) die ("convert: i/o error in base64 convert");
$first = true;
while (!feof($in))
{
$b64 = fread($in,filesize($in_fn));
// strip content tag from start of stream
if ($first)
{
$b64 = substr($b64,strpos($b64,',')+1);
$first = false;
}
$bin = base64_decode($b64);
if (!fwrite($out,$bin)) die("convert write error in base64 convert");
}
fclose($in);
fclose($out);
儘管此代碼產生腐敗的形象:
$in = fopen("in.b64", 'r');
$out = fopen("out.png", 'wb');
if ((!$in) || (!$out)) die ("convert: i/o error in base64 convert");
$first = true;
while (!feof($in))
{
$b64 = fread($in,3072);
// strip content tag from start of stream
if ($first)
{
$b64 = substr($b64,strpos($b64,',')+1);
$first = false;
}
$bin = base64_decode($b64);
if (!fwrite($out,$bin)) die("convert write error in base64 convert");
}
fclose($in);
fclose($out);
是的,如果你閱讀它分塊,你會打破編碼。 base64中的每個4個輸入字節表示3個輸出字節。不過,不包括空格。 – mario 2013-05-09 17:43:40