PHP - 分块文件副本(通过 FTP)缺少字节



因此,我正在编写一个分块文件传输脚本,旨在将文件(无论大小)复制到远程服务器。它几乎可以很好地工作(并且对我测试的 26 字节文件进行了测试,哈哈),但是当我开始做更大的文件时,我注意到它并不完全有效。例如,我上传了一个 96,489,231 字节的文件,但最终文件是 95,504,152 字节。我用一个 928,670,754 字节的文件对其进行了测试,复制的文件只有 927,902,792 字节。

还有其他人经历过吗?我猜feof()可能正在做一些不稳定的事情,但我不知道如何替换它或测试它。为了您的方便,我注释了代码。:)

<?php
// FTP credentials
$server = CENSORED;
$username = CENSORED;
$password = CENSORED;
// Destination file (where the copied file should go)
$destination = "ftp://$username:$password@$server/ftp/final.mp4";
// The file on my server that we're copying (in chunks) to $destination.
$read = 'grr.mp4';
// If the file we're trying to copy exists...
if (file_exists($read))
{
    // Set a chunk size
    $chunk_size = 4194304;
    // For reading through the file we want to copy to the FTP server.
    $read_handle = fopen($read, 'rb');
    // For appending to the destination file.
    $destination_handle = fopen($destination, 'ab');
    echo '<span style="font-size:20px;">';
    echo 'Uploading.....';
    // Loop through $read until we reach the end of the file.
    while (!feof($read_handle))
    {
        // So Rackspace doesn't think nothing's happening.
        echo PHP_EOL;
        flush();
        // Read a chunk of the file we're copying.
        $chunk = fread($read_handle, $chunk_size);
        // Write the chunk to the destination file.
        fwrite($destination_handle, $chunk);
        sleep(1);
    }
    echo 'Done!';
    echo '</span>';
}
fclose($read_handle);
fclose($destination_handle);
?>

编辑

我(可能已经)确认脚本以某种方式在最后死亡,并且没有损坏文件。我创建了一个简单的文件,每行对应于行号,最多 10000,然后运行我的脚本。它停在6253号线。但是,脚本在最后仍然返回"完成!",所以我无法想象这是一个超时问题。奇怪!

编辑 2

我已经确认问题存在于fwrite()的某个地方.通过在循环内回显$chunk,可以毫无故障地返回完整的文件。但是,写入的文件仍然不匹配。

编辑 3

如果我在 fwrite() 之后立即添加 sleep(1),它似乎可以工作。然而,这使得剧本需要一百万年才能运行。PHP 的追加是否有可能存在一些固有的缺陷?

编辑 4

好吧,不知何故,进一步将问题隔离为FTP问题。当我在本地运行此文件副本时,它工作正常。但是,当我使用文件传输协议(第 9 行)时,字节丢失了。尽管二进制标记了两种fopen()的情况,但这种情况仍在发生。可能是什么原因造成的?

编辑 5

我找到了解决方法。修改后的代码如上 - 我会尽快自己发布答案。

我找到了一个修复程序,尽管我不确定它为什么有效。只需在写入每个块后睡觉即可解决问题。我大大增加了块的大小以加快速度。虽然这可以说是一个糟糕的解决方案,但它应该适用于我的用途。无论如何,谢谢大家!

最新更新