我有一个 22M 的 docx 文件,想使用 php 中的 base64_encode() 函数对其进行编码。但运行此函数后它总是返回 NULL 值。此功能是否有任何限制文件大小或条件。我的代码:
$handle = fopen($fullpathfile, "rb");
$imagestr = base64_encode(fread($handle, filesize($fullpathfile)));
fclose($handle);
我有一个 22M 的 docx 文件,想使用 php 中的 base64_encode() 函数对其进行编码。但运行此函数后它总是返回 NULL 值。此功能是否有任何限制文件大小或条件。我的代码:
$handle = fopen($fullpathfile, "rb");
$imagestr = base64_encode(fread($handle, filesize($fullpathfile)));
fclose($handle);
试试这个代码
$fh = fopen($fullpathfile, 'rb');
$cache = '';
$eof = false;
while (1) {
    if (!$eof) {
        if (!feof($fh)) {
            $row = fgets($fh, 4096);
        } else {
            $row = '';
            $eof = true;
        }
    }
    if ($cache !== '')
        $row = $cache.$row;
    elseif ($eof)
        break;
    $b64 = base64_encode($row);
    $put = '';
    if (strlen($b64) < 76) {
        if ($eof) {
            $put = $b64."\n";
            $cache = '';
        } else {
            $cache = $row;
        }
    } elseif (strlen($b64) > 76) {
        do {
            $put .= substr($b64, 0, 76)."\n";
            $b64 = substr($b64, 76);
        } while (strlen($b64) > 76);
        $cache = base64_decode($b64);
    } else {
        if (!$eof && $b64{75} == '=') {
            $cache = $row;
        } else {
            $put = $b64."\n";
            $cache = '';
        }
    }
    if ($put !== '') {
        echo $put;
    }
}
fclose($fh);