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.
Related
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();
}
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!
I have two php functions that are called upon consecutive requestes:
This is called on the first request:
public static function ProcessResponseFile($request_file_content)
{
session_start();
$temp_file = tempnam("./tmp", "file");
file_put_contents($temp_file, $request_file_content);
$_SESSION['request_response'] = $temp_file;
return file_get_contents($temp_file);
}
The following function is called on the second request:
public static function GetResponseFileContent()
{
session_start();
$filename = $_SESSION['request_response'];
$handle = fopen($filename, "r");
$response_content = fread($handle, filesize($filename));
fclose($handle);
// $response_content = file_get_contents($_SESSION['request_response']);
unlink($_SESSION['request_response']);
unset($_SESSION['request_response']);
return $response_content;
}
The $response_content is empty. If I comment the unlink and unset, I get the appropriate file contents.
Why is that ?
I want to download the returned value of GetResponseFileContent()
if (isset($_POST['submit_download'])) {
ob_end_clean();
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=raspuns.xml");
header("Content-Type: application/octet-stream; ");
header("Content-Transfer-Encoding: binary");
session_start();
echo Application::GetResponseFileContent();
}
Before you can use ob_end_clean(), you have to use ob_start(). Otherwise, there's no output buffer to clear out.
I need some eduction please.
At the end of each month, I want to download some data from my webserver to my local PC.
So, I've written a little script for that, which selects the data from the DB.
Next, I want to download it.
I've tried this:
$file=$month . '.txt';
$handle=fopen($file, "w");
header("Content-Type: application/text");
header("Content-Disposition: attachment, filename=" . $month . '.txt');
while ($row=mysql_fetch_array($res))
{
$writestring = $row['data_I_want'] . "\r\n";
fwrite($handle, $writestring);
}
fclose($handle);
If I run this, then the file is created, but my file doesn't contain the data that I want. Instead I get a dump from the HTML-file in my browser..
What am I doing wrong..
Thanks,
Xpoes
Below script will help you download the file created
//Below is where you create particular month's text file
$file=$month . '.txt';
$handle=fopen($file, "w");
while ($row=mysql_fetch_array($res)){
$writestring = $row['data_I_want'] . "\r\n";
fwrite($handle, $writestring);
}
fclose($handle);
//Now the file is ready with data from database
//Add below to download the text file created
$filename = $file; //name of the file
$filepath = $file; //location of the file. I have put $file since your file is create on the same folder where this script is
header("Cache-control: private");
header("Content-type: application/force-download");
header("Content-transfer-encoding: binary\n");
header("Content-disposition: attachment; filename=\"$filename\"");
header("Content-Length: ".filesize($filepath));
readfile($filepath);
exit;
Your current code does not output a file, it just sends headers.
in order for your script to work add the following code after your fclose statement.
$data = file_get_contents($file);
echo $data;
I am creating a csv file from nested array and it works fine with a download link to the csv file in the localhost, but on live host it won't download. This is what is in my php file:
The headers declared:
/**
* Declare headers for CSV
*/
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=registration.csv");
header("Pragma: no-cache");
header("Expires: 0");
The Function that outputs the csv file:
/**
* Function will output the scsv file
* #param the csv $data in nested array form array(array("","",""),array("",""...)...)
*/
function outputCSV($data) {
$outstream = fopen("php://output", "w");
function __outputCSV(&$vals, $key, $filehandler) {
fputcsv($filehandler, $vals); // add parameters if you want
}
array_walk($data, "__outputCSV", $outstream);
fclose($outstream);
}
The link that I used in local:
Download CSV
Won't work on Live site. Instead of downloading it just takes me to the csv.php page and outputs the array in a string like this.
...ID,"Coach One","Coach Two",Work,Cell,Email,...
Try hardcoding the csv into the code. Replace these lines with the ones you have to see if your data being passed has bad characters. Maybe specifying the charset will help.
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));
As described here:
http://code.stephenmorley.org/php/creating-downloadable-csv-files/
I'm not setup to really debug your code. You can try this though if you like. I know it works.
$out = fopen("php://temp", 'r+');
while (($row = $res->fetch(Zend_Db::FETCH_NUM)) != false) {
fputcsv($out, $row, ',', '"');
}
rewind($out);
$csv = stream_get_contents($out);
header("Content-Type: application/csv;");
header("Content-Disposition: attachment; filename=\"foo.csv\"");
header("Pragma: no-cache");
header("Expires: 0");
echo $csv;
exit(0);
Adjust the loop as needed to iterate on your results.