PHP 向我上传的文件添加 2 个额外字节

标签 php upload

我有一个通过 PHP 上传文件的过程,但结果文件最终比源文件大 2 个字节。我不确定这 2 个字节是从哪里来的。 (实际过程是分块上传,我将一个文件切片并上传切片,每个切片最终到达的时间比开始时长 2 个字节,但我已经用一个小文件进行了测试,它到达时也比开始时大 2 个字节来源)。

我正在附加我的 PHP...这是 PHP 的正常功能吗?我在想象某种空终止符或其他东西(每个文件的末尾确实有一个\n 最初不存在)。在重新组装原始文件之前,我是否需要将文件读入缓冲区并删除最后两个字节?我不得不想象我做错了什么,但我对它会是什么感到困惑。

如果我确实需要手动去除最后两个字节,那么正确的方法是什么(这是一个二进制文件),然后将其余部分附加到我正在重建的整个文件中?


编辑


当 PHP 将其保存到服务器时,每个上传的文件都会在末尾添加一个 0D0A 字。所以...我想问题是如何防止这种情况发生。

<?PHP
$target_path = $_REQUEST[ 'path' ];
$originalFileName = $_REQUEST['original_file_name'];
$target_path = $target_path . basename( $_FILES[ 'Filedata' ][ 'name' ] );

if ( move_uploaded_file( $_FILES[ 'Filedata' ][ 'tmp_name' ], $target_path ) )
{

        $newFilePath = $originalFileName; //this is the overall file being re-assembled
        $fh = fopen($newFilePath, 'ab') or die("can't open file");

        $nextSlice = file_get_contents($target_path); //this is the slice that's 2 bytes too big each time

        fputs($fh, $nextSlice);
        fclose($fh);

//      unlink($target_path); //normally I'd delete the slice at this point, but I'm hanging on to it while I figure out where the heck the 2 extra bytes are coming from.

        fclose($fh);

        echo "SUCCESS";

}
else
{
     echo "FAIL:There was an error uploading the file, please try again!";
}
?>

最佳答案

文件是二进制文件吗?我在想 file_get_contents 会导致问题,因为它将它当作一个字符串来处理。也许你应该试试 fread相反?

关于PHP 向我上传的文件添加 2 个额外字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4858796/

相关文章:

c# - ASP.Net 处理程序 Request.Files 始终为空

php - XXHash for PHP(无模块/扩展)

javascript - 如何快速确定给定邮政编码的状态?

node.js - 有人知道如何使用 NodeJs 将音频/图像发送到 Telegram 机器人吗?

python - Django 无法上传多个文件并且 request.FILES 似乎为空

PHP 上传大图像并按比例调整大小,如 Photoshop

python - 在 Django 中上传时调整两个图像的大小

PHP分页,每页限制,mysql中的函数

PHP 表单空验证不起作用

php - 使用两个查询或允许 PHP 对 : Which is generally faster? 进行排序