PHP content disposition file name error safari - php

I have a database from which I would like the user to download data
I'm trying to download mp3 files. If the file contains, for example, Cyrillic characters, then in Safari I get the file ÐÐ¸Ð·Ð½ÐµÑ lite - ÐÑÐ°Ñ talk.mp3, in other browsers I get a normal file name Бизнес lite - Краш talk.mp3. Here is a sample code. Help, please, what am I doing wrong?
`
$src_file = ROOT_PATH . '/files/uploads/' . substr( $track_file['name'], 0, 2) . '/' . $track_file['name'];
if(file_exists($src_file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'. $track_file['title'] . '.mp3"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($src_file));
ob_clean();
flush();
readfile($src_file);
exit;
`

would use if else
if user agent safari . you can find browser name by the $_SERVER['HTTP_USER_AGENT']
$src_file = ROOT_PATH . '/files/uploads/' . substr( $track_file['name'], 0, 2) . '/' . $track_file['name'];
if(file_exists($src_file)) {
if($useragent=='safari'){
header('content-type: application/octet-stream');
header('content-Disposition: attachment; filename='.$src_file);
header('Pragma: no-cache');
header('Expires: 0');
readfile($src_file);
}else{
//your current code
}

Related

Using PHP Header to force download

I'm using this code to force a download using headers, the file name is renamed correctly but the file is empty, the actual file isn't being downloaded.
$file = url . '/uploads/audio/' . $trackFile;
$tmp = explode(".", $file);
$newTrackName = $trackTitle . '.' . end($tmp);
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $newTrackName);
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
You are missing the actual sending of the file e.g.
echo readfile($file);
exit;
or
header("Location: $file");
You need to add..
readfile($file);
exit;
You are correct and I upvoted both answers but I also found that the new version of cpanel turned of fopen by default, I had to enable fopen.

PHP - Forcing download a file not working

code.php
$code = $_GET["code"];
$file = 'code/'.$code.'.html';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
Generated URL to download the file:
http://www.example.com/code.php?code=yoursite.com_nbsp63ibrf
Well, I want to forcing download the html file, above code not working and it just preview the file at browser!
file_exists() returns false. Change your path with document root:
$code = $_GET["code"];
$file = $_SERVER['DOCUMENT_ROOT'] . '/code/' . $code . '.html'; // set your path from document root.
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
Try changing the content type to match the file type and setting the transfer encoding to binary:
header('Content-Transfer-Encoding: binary');
header('Content-Type: text/html');
Try this
$code = $_GET["code"];
$file = $_SERVER['DOCUMENT_ROOT'] . '/code/' . $code . '.html'; // set your path from document root.
if (file_exists($file)) {
header("Content-Type: text/html");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
unfortunately it was a encoding issue. I changed code.php encoding from UTF-8 to UTF-8 Without BOM then problem solved. Thanks for all answers and helps. I forgot that PHP header only works with UTF-8 Without BOM.
This is actually not possible.
The browser can always decide on its own, if the file should be downloaded or not.
The furthest you can go is sending the content-disposition header.

php download files not working on mobile

if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $filename. "." . $exts );
flush();
readfile($file);
exit;
}
this code works on pc, but on mobile i can not open the file. The headers is incorrect?
Try adding:
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
After
header('Content-Disposition: attachment; filename=' . $filename. "." . $exts );

Error: Duplicate headers received from server

Dublicate error happened in chrome, opera, safari and only works on firefox.
Here's the header:
| Header
| Forcing a download using readfile()
|----------------
*/
header('Content-Description: File Transfer');
header('Content-Type: ' . $file_mime_type);
header('Content-Disposition: attachment; filename=' . $file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $file_size);
ob_clean();
flush();
readfile($file_path);
exit;
I've tried to put double quotes and works but adds to file name - for e.g. -'filename.pdf'
header("Content-Disposition: attachment; filename= . '$file'");
To fix issue you need
// Change this
header('Content-Disposition: attachment; filename=' . $file);
// To this
header('Content-Disposition: attachment; filename="'.$file.'"');

Download content of several files on server with PHP

I have 2 files - 1.csv and 2.csv. I can download 1.csv with PHP by known approach:
$file = '1.csv';
header('Content-Description: File Transfer');
header('Content-Type: application/jpeg');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
But my purpose is to have in downloaded file content of both files - 1.csv and 2.csv. Does anybody have some ideas how to implement this?
Thank you in advance.
If I understand you correctly this should do it.
$file1 = '1.csv';
$file2 = '2.csv';
header('Content-Description: File Transfer');
header('Content-Type: text/comma-separated-values');
header('Content-Disposition: attachment; filename='.basename($file1, '.csv') . '-' . basename($file2, '.csv') . '.csv');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
header('Content-Length: ' . (filesize($file1) + filesize($file2)));
ob_clean();
flush();
readfile($file1);
readfile($file2);
Consider using a loop for multiple files and mind the change of the Content-Type header for CSV files.
$file1 = '1.csv';
$file2 = '2.csv';
header('Content-Description: File Transfer');
header('Content-Type: application/jpeg');
header('Content-Disposition: attachment; filename="1-2.csv"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: public');
header('Pragma: public');
header('Content-Length: ' . (filesize($file1)+filesize($file2)));
ob_clean();
flush();
echo file_get_contents($file1).file_get_contents($file2);
Downloads the concatenation of the 2 file contents

Categories