PHP | Download the zip file in php - 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);
?>

Related

Download file .ZIP with PHP automatically

I would like to download a file from a folder on my server and automatically start the download without the person seeing the original file link.
Here is my current script, but it downloads an invalid corrupted file.
$filename = "dsk.zip";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename="./zz/'.$filename.'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize("./zz/".$filename));
ob_end_flush();
#readfile("./zz/".$filename);
Can you please help me? The file, dsk.zip, is in the folder zz.
$file = "dsk.zip"; // this is what you will get from a param (i.e. ?file=dsk.zip) or from DB query
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='zz/'.$file");
header("Content-Length: ".filesize($file));
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
readfile('zz/'.$file);

How to load mp3 into JW Player using 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',

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?

Get content of a zip file and echo it to user

I wanna make a link for download and all of my file is zip but idont want to my user knows where is my download link
so i wanna get content of zip file and echo it with zip header to user can download it how can i do this?
You should use a "download.php?get=". base64_encode($filename)
When the user click on "download zip", you redirect him with header("Location: download.php?get=". base64_encode($url) to that page.
The headers, here we go
$filename = base64_decode($_GET[get]);
$filepath = "/var/www/domain/httpdocs/download/path/";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
ob_end_flush();
#readfile($filepath.$filename);
Instead of experimenting with headers you could try downloading some 'ready for use' php class. Then your could do something like:
$fileDown = new PHPFileDownload(null, null, 'myFileName', null, 'mypath/myfile.txt');
$fileDown->exportData();

Force downloading an image via php script works but the image only opens in paint not photoshop

Here is my php script which executes the download:
$link = 'www.example.com';
$url = urlencode($link);
$image = "http://chart.googleapis.com/chart?chs=75x75&cht=qr&chl=$url&choe=UTF-8&chld=L|0";
$filename = file_get_contents("$image");
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename='qr code';");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
echo $filename;
This downloads a file but it only opens in paint not photoshop. As a hint there doesn't appear to be a file type associated with the file (e.g. png or jpg).
I need all the headers above so this works in all browsers.
Thanks.
Perhaps pass a file extension in your filename header.
header("Content-Disposition: attachment; filename='qr_code.jpg';");

Categories