How to load mp3 into JW Player using php - php

I'm trying to load an mp3 into JW Player 5 using php to retrieve the actual file. This is the javascript code for the player:
jwplayer('mediaplayer').setup({
'flashplayer': 'player.swf',
'id': 'playerID',
'type': 'mp3',
'width': '600',
'height': '49',
'file': '/get_mp3/<?php echo $filename; ?>',
});
The file attribute has the URL to the PHP function (I use CakepHP, the part after the last / is the variable that the function gets passed).
This is the PHP function:
function get_mp3($filename) {
$file_path = '/path/to/files/' .$filename. '.mp3';
header("pragma : no-cache");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Type: audio/mpeg3");
header("Content-Disposition: inline; filename=" .$filename. ".mp3");
header("Content-Location: " .$filename. ".mp3");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file_path));
readfile($file_path);
}
If I call that function via web browser (for example mydomain.com/get_mp3/test_file) it prompts to download the right file, which seems to show that the PHP code is working.
However, when I use it on JW PLayer's file attribute it doesn't load anything or shows any kind of error.
I've tried adapting what's shown in this SO question but I couldn't make it work, I don't know if it's because for video it's different or because that mentoins JW PLayer 6 and mine is 5.
EDIT: test if it's an issue with CakePHP
So test if the issue is that for some reason CakePHP isn't working with JW Player I've put the following PHP code in an external PHP file:
$filename= 'my_filename';
$file = $_SERVER['DOCUMENT_ROOT']. '/path/to/files/' .$filename. '.mp3';
header("pragma : no-cache");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Type: audio/x-mp3");
header("Content-Disposition: inline; filename=" .$filename. ".mp3");
header("Content-Location: " .$filename. ".mp3");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile($file);
So now, the file line in the JW Player declaration reads like:
'file': '/get_mp3.php',
With that it still doesn't work. If I access the get_mp3.php page directly I get prompted to download the file, so again looks like the PHP code works...
EDIT 2
SO I've found the culprit of the issue: the path of the audio file. If I put the file in the root folder of the website, and I use the file path variable like $file_path = $filename. '.mp3'; it works fine.
The problem is that the audio files are in a different folder in the same server, and I can't move them... How can I change the PHP script to find the files in their current path? I've already tried with $file = '/path/to/files/' .$filename. '.mp3'; and $file = $_SERVER['DOCUMENT_ROOT']. '/path/to/files/' .$filename. '.mp3'; but it doesn't work...

Change your content type to ("Content-Type: audio/x-mp3");

So it turns out my problem was in the path to the file in the PHP script. I was using $_SERVER['DOCUMENT_ROOT'] and just / for the paths and it wasn't working, but then I realized that moving the file to the same folder as the script would work. In my case I still needed to find a way to access those files from a different folder, and using a relative path did the trick. Something like:
$filename= 'my_filename';
$file = '../../../path/to/files/' .$filename. '.mp3';
header("pragma : no-cache");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Type: audio/x-mp3");
header("Content-Disposition: inline; filename=" .$filename. ".mp3");
header("Content-Location: " .$filename. ".mp3");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile($file);

Since you are using JW5.
Under:
'file': '/get_mp3/<?php echo $filename; ?>',
Add:
'provider': 'sound',

Related

PHP | Download the zip file in php

I want to download the zip file in php.
Here is my code below.
<?php
ob_start();
// set example variables
$filename = "test.zip";
$filepath = "/home/somewhere/file/zip";
// http headers for zip downloads
header("Pragma: no-cache");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
readfile($filepath.$filename);
?>
And i have a link in my html code.
<html>
<head>
</head>
<body>
Download
</body>
</html>
The file is downloaded successfully when i clicked the link named 'Donwload' but i can't unzip and open the file.
The file name is test.zip when i downloaded for the first time and it is just the file name. Its extension is created when i click it to unzip.
The extension of file is ". cpgz" not the ".zip".
Here is the log when i downloaded it.
Resource interpreted as Document but transferred with MIME type application/zip:"myPHPfile.php"
Did i do something wrong in my code? or miss something?
How can i fix this?
My uploaded file is already zipped in the server and all i want to do is just download it.
The problem is your filepath, there's a missing trailing "/" after "/home/somewhere/file/zip" the working directory should be:
$filepath = "/home/somewhere/file/zip/";
Now this code works great!
<?php
$filepath = iconv("UTF-8","CP949", "/home/somewhere/zip/test.zip");
header("Pragma: no-cache");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=test.zip");
header("Content-Transfer-incoding: utf-8");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath));
ob_end_flush();
readfile($filepath);
?>

PHP - Generate file on the fly for android devices

I have read some answers here but i cannot seem to make my php script to work.
I generate a text or csv file (it depends what the user has chosen) from a form that it is submitted back to the same page.
The script works fine on Chrome, IE, Mozilla on my desktop but when i try on the stock browser on Android i get an attachment.html file that has the source of my script.
If i try with NEXT browser, i get a file with the proper filename but again inside is the source of my script.
I have read the following link but i cannot make it work
http://www.digiblog.de/2011/04/android-and-the-download-file-headers/
My http headers are the following
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . basename($FileName) . "\"");
header("Content-Length: " . filesize($FileName));
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Connection: close");
print $Content;
exit();
-----Update 1----
I wrote the following code to test things out
<?php
$get = $_GET['get'];
if ($get == 1) {
$content = 'This is a line of text!';
$filename = 'superfile';
header("Content-Description: File Transfer");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"" . basename($filename) . ".TXT\"");
header("Content-Transfer-Encoding: binary");
header("Expires: 0");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Connection: close");
echo $content;
exit;
}
echo '<html>
<head>
<title>Test</title>
</head>
<body>
<form method="get" action="index.php"
<label>Get</label>
<input type="text" name="get"/>
<input type="submit" name="ok" value="Send"/>
</form>
</body>
</html>';
?>
When i send the form with get method everything works, when i use post i get the html source that you see, same as the problem that i have with my original script.
-----Update 2-----
Okkkkk, now some more info, i think it is related to an Android bug, please check the following link
http://roscopeco.com/2013/04/android-and-the-form-post-download-problem/
Waiting for suggestions to work around this issue, i need method to me post because i pass too many varialbes and i don't want to have on big ugly url on the address bar.
Well, i didn't want to experiment too much so i get the user-agent and i change my post form to a get. Not a great fix but the job is done.

download file with php

i want to create a link to download an excel file from the root in server computer, using php. so i wrote a simple code as below,
<?php
header("Content-disposition: attachment; filename=example.xls");
header("Content-type: application/vnd.ms-excel");
readfile("example.xls");
?>
it then can be downloaded however when i want to open it, i got the error saying the file i downloaded is in a different format than specified by the file extension. i also tried the same method with jpeg file and didnt get the same error but when i click it, it shows nothing. can someone help me? im not very good with programming. thank you in advance!
Try this
$file='example.xls'; $filesize=filesize($file);
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: inline; filename="'.basename($file).'"');
header("Content-Length: " . $filesize);
$fh = fopen("$file, "rb");
// output file
while(!feof($fh))
{
# output file without bandwidth limiting
print(fread($fh, filesize($file)));
}
fclose($fh);

Why does Safari/WebKit appends .html to downloaded files?

I'm having lot of issues with Safari. When I provide a download from a PHP script the downloaded file has ".html" extension appended. Despite the HTTP headers I send to the client, I found them changed in inspector; in particular the mime-type is always set to text/html.
Here what I'm sending to the browser:
<?php
....
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: public, must-revalidate, post-check=0, pre-check=0");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=\"{$file['name']}\"");
header("Content-Type: ".$file['type']?$file['type']:'application/octet-stream');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($file['file']));
ob_end_flush();
#readfile($file['file']);
Where $file contains:
$file = array(
'name'=>'myfile.zip',
'file'=>'/tmp/myfile.zip',
'type'=>'application/zip'
);
What can I do?

File download is not working properly

I am trying to download a file using PHP. Now the file is downloaded , but it is not getting in the original format (extension missing). I can use the downloaded file after rename it using the original file extension. I am using the following code
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/".$result['ext']);
header('Content-length: '.filesize($file));
header('Content-disposition: attachment; filename='.$result['file_name']);
readfile($file);
exit;
where
$result['ext']="rar",
$file="file path to the uploaded folder".
I'm pretty sure you have to send the extension to the browser in the file name as part of your Content-disposition header.
For your code, you would have to change this:
header('Content-disposition: attachment; filename='.$result['file_name']);
to:
$filename = $result['file_name'] . '.' . $result['ext'];
header('Content-disposition: attachment; filename=' . $filename);

Categories