【问题标题】:Download mp3s from url array to zip file从 url 数组下载 mp3 到 zip 文件
【发布时间】:2015-05-05 00:47:17
【问题描述】:

我有一组指向 mp3 文件的链接。我需要从服务器下载所有 mp3,然后压缩。

下面的代码不起作用:

有什么建议吗?

$audio_flies = array('http://example.com/1.mp3','http://example.com/2.mp3');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($audio_files as $file) {
    echo "$file";
  $zip->addFile($file);
}
$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

存档格式未知或已损坏

【问题讨论】:

  • 你在这段代码的哪里下载文件?!你确定addFile()封装了下载过程吗? (我非常怀疑)
  • 到目前为止我还没有下载它们。这是我需要帮助的一种。抱歉,我的问题不太清楚。

标签: php download zip mp3


【解决方案1】:

您首先需要下载这些文件并将它们本地存储在您的服务器上。完成后,您将能够压缩它们并使 zip 文件可供下载。

实现这一点的最基本方法是使用 PHP 的 file_get_contents()file_put_contents()parse_url()

此代码尚未经过彻底测试。

// Fixed the name of the array
$audio_files = array('http://example.com/1.mp3', 'http://example.com/2.mp3');

// You need to change this to 
// a path where you want to store your 
// audio files (Absolute path is best)
$local_path = '/local/path/here/';
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);

// Download the files
// Not an elegant way to 
// download files
foreach ($audio_files as $file) {
    // First let's get the file name
    // by parsing the URL
    // and trimming the '/'
    $file_name = ltrim(parse_url($file, PHP_URL_PATH), '\/');

    // Download the file
    $a_contents = file_get_contents($file);

    // Place the downloaded content
    // in the defined local path
    file_put_contents($local_path . $file_name, $a_contents);

    // Add the file to archive
    $zip->addFile($local_path . $file_name);
}

$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

是一种非常糟糕的方式来完成您的任务,原因如下(以及许多其他原因):

  1. 下载过程没有超时
  2. 如果文件下载失败,它将停止执行
  3. 提供 zip 文件可能需要很长时间(取决于音频文件的大小)
  4. 没有适当的错误/异常处理程序
  5. 关于结构的许多其他原因,我不会在这里讨论。

【讨论】:

  • 感谢您提供非常有用的信息 BassemDy。您有什么建议可以将许多 mp3 URL 压缩到一个 zip 中吗?来自 URL 的?
  • mp3 的数据库将发生变化,因此它需要是动态的,但是,也许我可以每天运行一次脚本并将 zip 存储在服务器上,然后链接以下载该 zip?跨度>
  • 是的,您可以使用像 wget 这样的 unix 工具通过 cronjob 运行来下载和压缩这些文件并将它们存储在本地目录中。然后,您将根据需要从磁盘提供 zip 文件,而不是每次都根据请求创建它!
【解决方案2】:

尝试以下操作:使用 parse_url 获取实际文件名,使用 file_get_contents 和 file_put_contents 下载并保存数据到本地...

$audio_flies = array('http://example.com/1.mp3','http://example.com/2.mp3');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($audio_files as $file) {
  echo "$file";
  $parsed = parse_url($file); 
  $localfile = "." . $parsed["path"];
  $mp3 = get_file_contents($file);
  file_file_contents($localfile, $mp3);
  $zip->addFile($localfile);
}
$zip->close();

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

【讨论】:

  • 我收到此代码的致命错误:致命错误:调用未定义函数 get_file_contents()
  • 我对这个答案投了反对票,因为您使用了两个未定义的函数,并且您没有注意到数组名称的拼写错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多