I want to get information and download it through a file. But it just open a new window and show the information in it.
$filename = $this->input->cookie('merchant_id');
$timestamp = date("YmdHis",time());
$filename .= $timestamp.'.'.$fileType;
$title = mb_convert_encoding($title,'gbk','utf-8');
header('Content-type: application/octet-stream');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Content-Disposition: attachment; filename=' . $filename);
echo $title;
$ret = $this->download_model->QueryList($Req, $Resp);
$content = $this->GetDownLoadContentNew($ret['bill_list'], $fileType);
echo $content;
The GetDownLoadContentNew function just format the content.
So, why it cannot download?
Related
$ci =& get_instance();
$ci->load->dbutil();
$result = $ci->dbutil->csv_from_result($data);
$filename = "sample.csv";
$fp = fopen('php://output', 'w');
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename.'";');
header('Cache-Control: must-revalidate,post-check=0, pre-check=0');
header("Pragma: no-cache");
header("Expires: 0");
foreach ($final_res as $line){
fputcsv($fp,explode(',',$line));
}
fclose($fp);
exit;
I am not able to download the CSV file. I couldn't find any errors. please help
Why not just use force_download() helper function of CI. LINK
Try changing header in your code.
header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
I have created the text file and downloading when I execute the code. But I need to store this downloaded file in a specified folder inside the server. I have tried in many ways, but I didn't get any solution. I have mentioned my code below, Can anyone guide me?
$saving_name = $i.''.$machineid.'.'.$count_pad;
foreach($data['SalesDetails'] as $d)
{
$val = $d->inv_total - $d->inv_discount;
$explodedval = explode(".",$val);
$totalval = str_pad($explodedval['0'], 8, "0", STR_PAD_LEFT);
$totalvals = str_pad($explodedval['1'], 2, "0", STR_PAD_RIGHT);
$result = $totalval.'.'.$totalvals;
$dates = str_replace("-","",$d->date);
$out .= $i.''.$machineid.''.$dates.''.$result.' '."\r\n";
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$saving_name.'.txt');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $out;
exit;
Here is the path need to move the downloading file, /var/www/html/art/assets/uploads/edi/sample.txt
Thank you for your support. Finally I got solution for this which is shown below:
$saving_name = $i.''.$machineid.'.'.$count_pad;
foreach($data['SalesDetails'] as $d)
{
$val = $d->inv_total - $d->inv_discount;
$explodedval = explode(".",$val);
$totalval = str_pad($explodedval['0'], 8, "0", STR_PAD_LEFT);
$totalvals = str_pad($explodedval['1'], 2, "0", STR_PAD_RIGHT);
$result = $totalval.'.'.$totalvals;
$dates = str_replace("-","",$d->date);
$out .= $i.''.$machineid.''.$dates.''.$result.' '."\r\n";
}
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$saving_name.'.txt');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $out;
$backup_file = '/var/www/html/artbak/assets/uploads/edi/'.$saving_name.'.txt';
//save file
$handle = fopen($backup_file,'w+');
fwrite($handle,$out);
fclose($handle);
exit;
Now the file stored in the specified folder as well download also.
I'm trying to force download of .dwg files with the following code:
$filename = $_GET['f'];
$upload_path = wp_upload_dir();
$src = $upload_path['basedir'] . '/' . $filename;
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header("Content-disposition: attachment; filename=\"{$filename}\"");
header("Content-Type: application/octet-stream");
header('Content-Transfer-Encoding: binary');
readfile($src);
die();
The path to the file is correct, but it only returns a file that is 6 byte and that is not possible to open. It should be 754 bytes. Anyone who can help me out here?
//Example file
$src = "/var/www/wp/wp-content/uploads/Glasparti-modell-Silence.dwg";
Try to add the Content-length header :
$size = filesize($src);
header("Content-length: $size");
Try with:
$name = $_GET['f'];
$url = './files/'.$name;
header("Content-type: application/octet-stream");
header("Content-disposition: attachment;filename=$name");
readfile($url);
I solved this by the following in .htaccess instead:
<FilesMatch "\.(dwg)$" >
ForceType application/octet-stream
Header add Content-Disposition "attachment"
</FilesMatch>
Can you please use .dwg file Content-Type:
application/acad
application/x-acad
application/autocad_dwg
image/x-dwg, application/dwg
application/x-dwg
application/x-autocad
image/vnd.dwg
drawing/dwg
header("Content-type: 'application/pdf'");
header("Content-Type: application/force-download");
Can you try this,
<?php
$file = '';// file path here
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}else{
echo "File not found";
}
?>
Try it !!
/**
* file download in MVC
*
* #author VECTOR Ann <ann#vector.cool>
* #since 1.0.1
*
* #param string $file 提供下載的檔案絕對路徑
* #param string $download_name 下載的檔名
* #return void
*/
function v_file_download($file,$download_name=''):void
{
if (is_file($file)) {
if($download_name=='')$download_name = date('Y_m_d',time()).".zip";
header_remove();
ob_end_clean();
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($download_name));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
ob_end_flush();
exit;
}
}
I have a problem with download script
when I download any file it is save with 0kb.
Any ideas? Here is my PHP script
<?php
$file = $_GET['fname'];
$fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullpath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
ob_clean();
flush();
readfile($fullpath);
?>
Please add content-length header as well .
Please try this:-
<?php
$file = $_GET['fname'];
$fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf";
$headers = get_headers($fullpath, 1);// use file absolute path here
$fsize = $headers['Content-Length'];
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullpath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header("Content-Length: " . $fsize);
ob_clean();
flush();
readfile($fullpath);
?>
Try below code hope it will help you.
I have tested this in my localhost.
$file = $_GET['fname'];
$fullpath = get_bloginfo('template_url')."/application/display/1358947404.pdf";
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($fullpath));
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($fullpath));
ob_clean();
flush();
readfile($fullpath);
I need to know how to determine how many bytes sent over http.
This is what I did so far.
ignore_user_abort(true);
header('Content-Type: application/octet-stream; name="file.pdf"');
header('Content-Disposition: attachment; filename="file.pdf"');
header('Accept-Ranges: bytes');
header('Pragma: no-cache');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-transfer-encoding: binary');
header('Content-length: ' . filesize('file/test.pdf'));
readfile('file/test.pdf');
if (connection_aborted()) {
$transfer_success = false;
$bytes_transferred = ftell($handle);
echo $bytes_transferred;
die();
}
Can anyone help me out ?
Thanks
Take a look at this other post of the same question:
PHP - determine how many bytes sent over http
Code example taken from the linked page (originally posted by J., modified to fit your example):
ignore_user_abort(true);
$file_path = 'file/test.pdf';
$file_name = 'test.pdf';
header('Content-Type: application/octet-stream; name=' . $file_name );
header('Content-Disposition: attachment; filename="' . $file_name . '"');
header('Accept-Ranges: bytes');
header('Pragma: no-cache');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Content-transfer-encoding: binary');
header('Content-length: ' . $file_path);
$handle = fopen($file_path, 'r');
while ( ! feof($handle)) {
echo fread($handle, 4096);
if (connection_aborted()) {
$transfer_success = false;
$bytes_transferred = ftell($handle);
break;
}
}
fclose($handle);
Try to use the return value of readfile:
$bytes_transferred = readfile('file/test.pdf');
$transfer_success = ($bytes_transfered == filesize('file/test.pdf'));
if (!$transfer_success) {
// download incomplete
}