简体   繁体   中英

php mp3 downloads html file on android

Here's my code:

<?php
$download = './downloads/'.$mp3;

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=".basename($download)); 
header("Content-Type: audio/mpeg");
readfile($download);
?>

On android, once download starts, it downloads an unreadable htm file. $mp3 is like this: NAME.mp3. What may be wrong?

Hmmm, have you looked at your PHP error logs? This sounds to me like your server / dev environment doesn't have PHP running correctly.

I found this post relating, seems they were able to get it to work with some extra headers, that is if your PHP is working properly:

https://stackoverflow.com/a/2367506/2080324

Yes, but remember headers tell devices what type of file it is. Different devices will interpret different files in different ways. In the example I linked, he sets the mime-type as well as headers:

$dir = dirname($_SERVER['DOCUMENT_ROOT'])."/protected_content";
$filename = $_GET['file'];
$file = $dir."/".$filename;

$extension = "mp3";
$mime_type = "audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3";

if(file_exists($file)){
    header('Content-type: {$mime_type}');
    header('Content-length: ' . filesize($file));
    header('Content-Disposition: filename="' . $filename);
    header('X-Pad: avoid browser bug');
    header('Cache-Control: no-cache');
    readfile($file);
}else{
    header("HTTP/1.0 404 Not Found");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM