PHP中不同格式的base64_decode函数有区别吗

Is there a difference in base64_decode function for different formats in PHP

我正在尝试使用以下 php 函数将一些图像上传到服务器:

function upload($output_file, $base64_string)
{
        $ifp = fopen($output_file, "wb");

        $data = explode(',', $base64_string);

        fwrite($ifp, base64_decode($data[1]));
        fclose($ifp);

        return $output_file;
}

当我将 jpeg 图片传递给函数时,一切正常,但是当我传递 .png 或其他东西时,上传的图片已损坏。

不同文件类型解码base64字符串有什么区别吗?

不,base64_encode()base64_decode() 不是上下文感知的。
Base64 格式最初甚至不是为了在网页上有效地显示图像而设计的 - 今天恰好可以用于此。

您的问题是由损坏的数据引起的 - 不一定损坏,但您传递给 base64_decode() 的不是您最初从 base64_encode() 获得的结果。
我假设您以某种方式通过 HTTP 传输它,并且通常涉及 URL 编码,这将编码 +/= - 完全有效的 Base64 字符。