So The thing in a table there are some checkboxes so based on the checkboxes user have checked it will downnload a particular coloumn of the table in the .php format. So I have tried the following code:-
public function export_php() {
$export = $this->session->userdata('export_id');
$exports = $this->query_revision_model->export($export);
$data = "";
foreach ($exports as $export) {
$data .= $export->sql_changes . "\r\n";
}
$filename = "export.php";
$handle = fopen($filename, w);
fwrite($handle, $data);
fclose($handle);
}
So,Everything is working fine but I'm unable to provide the path for the download files,So it's saving on the application folder which I don't want.
I tried force_download($filename,$data) did n't work because I am using ajax.
You need to force the browser to download file except display.
public function export_php() {
$export = $this->session->userdata('export_id');
$exports = $this->query_revision_model->export($export);
$data = "";
foreach ($exports as $export) {
$data .= $export->sql_changes . "\r\n";
}
$filename = "export.php";
$handle = fopen($filename, w);
fwrite($handle, $data);
fclose($handle);
$filePath = APPPATH.$fileName;
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Type: application/php");
// Read the file
readfile($filePath);
exit;
}
and in your ajax code try this instead of ajax
window.location.replace('Your URL Here')
Hope it helps!
Related
Could someone help me with this problem?
I have following function
public function exportcsv(){
//echo 'hi'; exit();
// file name
$filename = 'blocked_users_'.date('Y-m-d').'.csv';
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/csv; ");
// get data
$user_data = $this->Common_model->get_users_for_csv();
// file creation
$file = fopen($filename, 'w');
$header = array("full_name", "phone", "email", "payment_method", "delivery_address", "delivery_city", "timestamp");
fputcsv($file, $header);
foreach ($user_data as $key=>$line){
fputcsv($file,$line);
}
fclose($file);
exit;
}
File it's generated 2 times, one, on server root second it's downloaded to disk, but on server root file it's ok with headers and content, the second one which is downloaded on disk it's empty.
I am trying to export few data in CSV file. Though the file is successfully downloaded as well as stored in particular path. If I opened downloaded file from the browser, file showing empty results I don't know where I made mistake please help me out
My Controller
public function export_webdata($p_id,$inptid){
$project = $this->Lead_Model->get_project_single($p_id);
$filename = $project[0]->p_name.'_WebsiteInfo_'.time().'.csv';
$path = getcwd().'/public/files/'.$project[0]->p_name.'/';
if (!file_exists($path)){
mkdir($path, 0777, true);
}
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'";');
$output = fopen($path.$filename, 'w');
$header = array("Company","Country","Website");
fputcsv($output, $header);
$extdata = $this->Lead_Model->webdata_export($inptid);
foreach ($extdata as $key=>$line){
fputcsv($output,$line);
}
fclose($output);
}
My Model
public function webdata_export($inptid)
{
$response = array();
$db5 = $this->load->database('output_db', TRUE);
$q = $db5->select('Company,Country,Website')->get_where('website_info', ['input_id' => $inptid, 'type_id' => 2,'uploaded_by' => $this->session->admin_id]);
$response = $q->result_array();
return $response;
}
you need to add below code to clean buffer before or after header
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$filename.'";');
while ( ob_get_level() ) {
ob_end_clean();
}
I am uploading pdf file as BLOB type in mysql. Now I want to download that file using hyperlink tag. How to do that?. The below code is not working.
Upload code
function do_upload_result($img){
$files = $_FILES[$img];
$ci = & get_instance();
$ci->load->library('upload');
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'pdf';
$_FILES[$img]['name'] = time().'_'.$files['name'];
$filename=$_FILES[$img]['name'];
$_FILES[$img]['type'] = $files['type'];
$_FILES[$img]['tmp_name'] = $files['tmp_name'];
$_FILES[$img]['error'] = $files['error'];
$_FILES[$img]['size'] = $files['size'];
$ci->upload->initialize($config);
if ($ci->upload->do_upload($img))
{
$prod_img = array('upload_data' => $ci->upload->data());
}
else
{
}
return $ci->upload->data();
$prod_img = 'marksheet';
$prodimg= do_upload_result($prod_img);
$imgdata = file_get_contents($prodimg['full_path']);
Download
Assuming that you've a controller called Test and it has method called download(). So then the url would be as the following as you tagged the question with CodeIgniter:
Download PDF
Next up, pull off BLOB data for the file from the database and put it inside the method. Assuming you are getting $value['marksheet']; from your database.
<?php
function download() {
// Prevents out-of-memory issue
if (ob_get_level()) {
ob_end_clean();
}
// Here you should prepare $value['marksheet'];
// Notice I passed $value['marksheet'] in base64_encode
$encoded_data = base64_encode($value['marksheet']);
// Decodes the encoded data
$decoded_data = base64_decode($encoded_data);
// Set a path where file_put_contents() will create a file
$file = APPPATH . '../upload/temporary_file.pdf';
// Writes data to the specified file
file_put_contents($file, $decoded_data);
// Modify the name as you want
$filename = 'download_name.pdf';
header('Expires: 0');
header('Pragma: public');
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '"');
readfile($file);
if (file_exists($file)) {
unlink($file);
}
}
With PHP I'm opening a .csv file, I'm adding som rows in it then I'm downloading the new .csv file.
the code I'm using is:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
$file = fopen("csv.csv","r");
$list = array();
while(! feof($file))
{
$list[] = (fgetcsv($file));
}
fclose($file);
$list[] = array('name5', 'town5');
$list[] = array('name6', 'town6');
$list = array_filter($list);
outputCSV($list);
function outputCSV($list) {
$output = fopen("php://output", "w");
foreach ($list as $row) {
fputcsv($output, $row);
}
fclose($output);
}
My issue is the other PHP code in the page don't generate, and only the .csv file is being download.
for exemple if I'm adding:
echo "test";
it won't be display and only csv.csv will be downloaded
How can I display my "echo test;" ?
With the headers you are telling the browser to download a CSV, there is no where to output your echo.
You could make a page with the information you want to show that has an iframe with the csv code in it.
I export csv in php as:
$fp = fopen($filename, 'w');
foreach ($list as $fields) {
if(!empty($fields))
fputcsv($fp, $fields);
else
fputcsv($fp, ' ');
}
$fp = chr(255).chr(254).mb_convert_encoding($fp,"UTF-16LE","UTF-8");
fclose($fp);
When i open csv, font UTF_8 is error.
Ex: 防本部コー show in file csv: æ¶ˆé˜²æœ¬éƒ¨ă‚³ăƒ¼ăƒ‰
Can I help you? Thanks
This has been discussed here:
How can I output a UTF-8 CSV in PHP that Excel will read properly?
You might also want to check your CSV in an alternative text editor to rule out the editor.
$fp is a pointer resource to a file. It is not a string, you cannot do this:
$fp = chr(255).chr(254).mb_convert_encoding($fp,"UTF-16LE","UTF-8");
To write UTF-16 data to the file, you need to convert it before writing it, not after the fact:
$fp = fopen($filename, 'w');
fwrite($fp, chr(255).chr(254));
foreach ($list as $fields) {
foreach ($fields as &$field) {
$field = mb_convert_encoding($field, 'UTF-16LE', 'UTF-8');
}
fputcsv($fp, $fields);
}
fclose($fp);
Don't know if this actually outputs the data you need, but it fixes the obvious error.
Try this function will work:
public static function exportCSVFile($data = array(), $headers = array(), $delimiter = ",", $csvfilename = "CSVFile") {
ob_clean();
ob_start();
$csvfilename = $csvfilename . "_" . date("Y_m_d_h_i_s") . ".csv";
$file_handler = fopen("php://output", 'w');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/csv");
//header( "Content-Type: application/csv;charset=utf-8" );
header("Content-Disposition: attachment;filename={$csvfilename}");
header("Content-Transfer-Encoding: binary");
foreach ($data as $fields) {
$_str = implode($delimiter, $fields);
$fields = explode($delimiter, utf8_decode($_str));
fputcsv($file_handler, $fields, $delimiter, $enclosure = '"');
}
fclose($file_handler);
ob_flush();
exit();
}