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);
Related
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);
?>
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',
I have a simple web page which pulls some data from DB and displays it in a html table and stores the data in some session variables.
I have a button "Export to csv" which calls a page which exports the results to a csv file.
The code for ExportToCsv file is:
<?php
session_start();
include "conn/sqlserverConn.php";
function download_send_headers($filename) {
// disable caching
$now = gmdate("D, d M Y H:i:s");
header("Expires: Tue, 03 Jul 2001 06:00:00 GMT");
header("Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate");
header("Last-Modified: {$now} GMT");
// force download
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
// disposition / encoding on response body
header("Content-Disposition: attachment;filename={$filename}");
header("Content-Transfer-Encoding: binary");
}
$data = $_SESSION['data'];
$rows = $_SESSION['row'];
$header = $_SESSION['header'];
download_send_headers("data_export_" . date("Y-m-d") . ".csv");
$file = fopen("php://output", 'w');
fputcsv($file,$header);
for($i=0; $i<$rows; ++$i)
{
$valuesArray=array();
foreach($data[$i] as $name => $value)
{
$valuesArray[]=$value;
}
fputcsv($file,$valuesArray);
}
fclose($file);
die();
?>
My code is working flawlessly in firefox but it is not working in chrome or IE. On chrome and IE it is showing error 404 (Object not found!). Please tell me what is the problem ?
As outlined in the blog article at: http://www.exchangecore.com/blog/php-output-array-csv-headers/, I tested the following and it worked in IE8-11, and in chrome version 34. I presume it will work in other browsers fine as well.
Headers to use:
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=' . $fileName);
<?php
$docName = "testdoc";
header("Expires: Mon, 26 Jul 2020 05:00:00 GMT");
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/msword; charset=ISO-8859-1");
header("Content-Disposition: attachment; filename=\"".$docName.".doc");
#/ Create and format your text using HTML, its simple stuff ... trial and error ;)
echo '<img src="logo.gif" />';
?>
Soumya Sarkar
Hi I got this following code from google to generate a Doc file by php. But I am unable to put any image there. Can anybody help me with it?
It requires the link, you will need to host the images online in order for the image to be shown on the doc.
Try this and it is working:-
echo "<img src='http://i.imgur.com/MJ5Sa.jpg' />";
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);