【问题标题】:In PHP, output as byte array and stream. Which one is better?在 PHP 中,输出为字节数组和流。哪一个更好?
【发布时间】:2011-05-31 03:52:04
【问题描述】:

我需要编写一个 php Web 服务来将文件输出到 Windows 客户端应用程序。我有两个选择字节数组和流。 PHP 中哪一个更好更容易实现?

感谢您的帮助。

【问题讨论】:

  • 字节数组和流是什么意思?
  • 我可以将文件作为 ByteArray 发送到客户端或将其流式传输出去

标签: php service stream bytearray


【解决方案1】:

不如……

$file = 'some_file.exe';

$_size = filesize($_file);
header('Content-Type: binary/octet-stream');
header('Content-Length: '.$_size);
header('Content-Disposition: attachment; filename="' . basename($file) . '"; size=" . $_size);

@readfile($file);

注意:请参阅关于 some issues with binary files 的评论。

【讨论】:

  • 你应该使用readfile而不是echo来读取内容。不过,我认为这并不是 OP 真正要求的。
  • @deceze:这很有趣(可能是因为疲劳)我先想file()(知道那不对)然后去了file_get_contents()readfile() 是,你是对的,更合适。但是,我更多地只是表明您将在标题和输出内容之间进行组合以获得所需的结果。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
  • 2010-10-23
  • 1970-01-01
相关资源
最近更新 更多