【问题标题】:download audio with wget over php使用 wget over php 下载音频
【发布时间】:2018-04-14 16:22:40
【问题描述】:

我正在尝试通过 php 下载音频文件。我试过用 curl 和 wget 下载它。以下是示例:

<?php
  exec('wget http://static1.grsites.com/archive/sounds/cartoon/cartoon001.wav');
?>

如何使用上述两种方法之一下载它。

【问题讨论】:

  • 您是想将该文件放到服务器上还是访问该 php 文件的人那里?
  • How to use wget in php?的可能重复
  • 给访问文件的人。我想把它下载到 /home/name/Downloads
  • @psrcek 我已尝试关注如何在 php 中使用 wget。但我有 wav 文件而不是 xml。
  • 你想要什么xml?从什么时候开始 wav 文件包含 xml?

标签: php curl wget


【解决方案1】:

我认为这应该适合你:

<?php
    $url = 'http://static1.grsites.com/archive/sounds/cartoon/cartoon001.wav';
    header('Content-Type: audio/wav');
    header('Content-Disposition: inline;filename="name.wav"');
    header('Content-length: '.get_headers($url,1)['Content-Length']);
    header('Cache-Control: no-cache');
    header("Content-Transfer-Encoding: chunked");
    readfile("http://static1.grsites.com/archive/sounds/cartoon/cartoon001.wav");
?>

【讨论】:

  • 下载它只是第一行。所有其他线路都只是为它服务。此外,这是一种糟糕的、非常缓慢且占用内存的方法,首先在开始为页面提供服务之前,您下载整个文件,并在提供之前将整个文件一次存储在内存中。一种更快、更少内存占用的方法是使用 readfile() 而不是 file_get_contents() - 然后在下载前几个字节后页面将开始提供服务,并且文件的一小部分保留在在任何给定时间在服务器上运行
  • 如果你删除$audio = file_get_contents('http://static1.grsites.com/archive/sounds/cartoon/cartoon001.wav');,并用readfile("http://static1.grsites.com/archive/sounds/cartoon/cartoon001.wav");替换echo $audio;,页面加载速度会比file_get_contents方法快得多,并且使用的内存要少得多,它也会使用相同的数量ram,无论目标文件有多大。而 file_get_contents 代码根据音乐文件的大小使用更多内存。音乐文件是100MB吗? file_get_contents 将使用 100MB 内存,readfile() 将使用几千字节内存。
  • @hanshenrik 你不能设置 Content-length 标题。客户端不知道数据的大小,通常会显示“剩余时间未知”。
  • @psrcek header('Content-length: '.get_headers($url)['Content-Length']);
猜你喜欢
  • 2012-12-31
  • 2012-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多