Php 为什么base64_encode()返回null

Php 为什么base64_encode()返回null,php,Php,我有一个22M docx文件,希望在php中使用base64_encode()函数对其进行编码。但在运行此函数后,它总是返回空值。此函数是否有任何限制文件大小或条件。我的代码: $handle = fopen($fullpathfile, "rb"); $imagestr = base64_encode(fread($handle, filesize($fullpathfile))); fclose($handle); 试试这个代码 $fh = fopen($fullpathfile, 'rb

我有一个22M docx文件,希望在php中使用base64_encode()函数对其进行编码。但在运行此函数后,它总是返回空值。此函数是否有任何限制文件大小或条件。我的代码:

$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); 
$fh=fopen($fullpathfile,'rb');
$cache='';
$eof=假;
而(1){
如果(!$eof){
如果(!feof($fh)){
$row=fgets($fh,4096);
}否则{
$row='';
$eof=真;
}
}
如果($cache!='')
$row=$cache.$row;
elseif($eof)
打破
$b64=base64_编码($row);
$put='';
如果(斯特伦($b64)<76){
如果($eof){
$put=$b64。“\n”;
$cache='';
}否则{
$cache=$row;
}
}elseif(斯特伦($b64)>76){
做{
$put.=substr($b64,0,76)。“\n”;
$b64=substr($b64,76);
}而(斯特伦($b64)>76);
$cache=base64_解码($b64);
}否则{
如果(!$eof&&$b64{75}='='='){
$cache=$row;
}否则{
$put=$b64。“\n”;
$cache='';
}
}
如果($put!=''){
回音$put;
}
}
fclose($fh);

fread返回什么?您是否“知道”它是docx文件,或者您实际测试过它?从不返回
null
。但是,如果失败,它将返回
false
。22M是一个大文件,可能您在某个地方“内存不足”。@GolezTrol fread也将返回NULL。docx fileFirst将文件内容保存在另一个变量中,并对其进行回显。我猜这里有点不对劲,因为
base64\u encode()
请在代码中提供注释来解释它是如何工作的:)