如果在使用“readfile”发送文件时我修改了文件,会下载失败吗?

发布于 2024-11-16 06:44:04 字数 86 浏览 0 评论 0原文

如果我使用 readfile() 向浏览器发送一个大文件(200mb),并且在用户下载时修改该文件,下载会成功完成吗?如果不是 - 如何修改不断下载的大文件?

If I send a big (200mb) file to the browser with readfile(), and while the user is downloading I modify the file, will the download finish succesfully? If not - how do I modify large files that are constantly being downloaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

零崎曲识 2024-11-23 06:44:04

不可以,直接编辑文件数据是无法成功完成的。将收到损坏的文件。

但是...

在 Linux 中,如果您 rmmv 文件,任何已打开该文件的程序都将继续访问它。只有当文件被关闭时,文件才被完全释放。因此,您可以通过以下步骤安全地编辑大文件:

  1. 将文件复制到临时名称。
  2. 编辑文件。
  3. rm file_being_downloaded; mv new_file file_being_downloaded;

我还没有对此进行测试,但这应该允许所有下载 200mb 文件的人完整地接收其副本,并且新的下载者将获得您的更新版本。

No, it will not finish successfully if you edit the file data directly. A corrupted file would be received.

However...

In Linux, if you rm or mv a file, any program that already has that file open will continue to access it. It is only once the file is closed that the file is completely released. Therefore, you could safely edit your large file with these steps:

  1. Copy the file away to a temporary name.
  2. Edit the file.
  3. rm file_being_downloaded; mv new_file file_being_downloaded;

I have not tested this, but that should allow all people downloading the 200mb file to receive their copy completely in tact, and new downloaders will get your updated version.

吲‖鸣 2024-11-23 06:44:04

我无法对此进行测试,但很可能下载会损坏,并且 readfile() 将返回 false 并引发错误。另一种选择是,iff readfile() 锁定文件,在文件读取完成之前您将无法写入该文件- 但我认为情况并非如此。

如果我是你,我会复制该文件,提供它,然后将其删除。您可能还想将文件内容加载到内存中并提供服务,但对于 200MB 的文件,这是不切实际的......

$file = '/path/to/file.200mb';
$temp = tempnam(sys_get_temp_dir(), time() . '_');

copy($file, $temp);    
readfile($temp);    
unlink($temp);

I can't test this but most probably the download will get corrupted and readfile() will return false and throw an error. Another option, iff readfile() locks the file is that you won't be able to write to the file until it has finished reading it - I don't think this is the case though.

If I were you I would duplicate the file, serve it and then delete it. You might also want to load the file contents into memory and serve that, but for a 200MB file this would be unpractical...

$file = '/path/to/file.200mb';
$temp = tempnam(sys_get_temp_dir(), time() . '_');

copy($file, $temp);    
readfile($temp);    
unlink($temp);
淡莣 2024-11-23 06:44:04

我想说这取决于下载的处理方式。

如果文件保存到服务器上的单独内存位置,并且您修改了实际文件。鉴于用户正在尝试将文件下载到备用内存位置,因此不应对其产生影响。

I would say it depends on how the download is being handled.

If the file is saved to a separate memory position on the server, and you modify the actual file. Seeing as the user is trying to download the file in the alternate memory position, it shouldnt affect it.

一身仙ぐ女味 2024-11-23 06:44:04

最好的办法是建立一个新文件,然后更新代码以加载新文件。这样,下载旧文件的用户将继续这样做,但您将能够转换到新文件。

Your best bet is to put up a new file, and then update the code to load the new file. That way users who are downloading the old file will continue to do so, but you will be able to transition to a new file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文