1

In my code I am creating a file with fgetcsv. I download it and open the file but all the html from my page is included with the csv file. Is there anyway I can just have the data I want to output from the file and not the extra html. my function for creating the csv is below

function makefile()
{
    header('Content-type: text/csv');
    header("Cache-Control: no-store, no-cache"); 
    header("Content-Disposition: attachment;filename=file.csv");

    $this->filepointer  =   fopen('php://output', 'a');



    for($count=0;$count < count($this->alldata);$count++)
    {
       fputcsv($this->filepointer, $this->alldata[$count]);
    }

    fclose($this->filepointer);

}

also I was wondering if i use $this->filepointer = fopen('file.csv', 'w') will the file still be populated with the html. as I dont have to have the file downloading I was just using it to check if the file was being created in the correct format. thanks again

eoin
  • 113
  • 2
  • 3
  • 20
  • this code could not be including html, unless it's part of that `$this->addata`. it's probably your download page being implemented incorrectly, wrapping the file in html. – Marc B Mar 11 '13 at 16:23
  • Hi, yeah the function is being called on a page with html code on it. "$this->alldata" is an array of arrays. tried echoing it out with var_dump() and It definately only contains useful data, no html etc. – eoin Mar 11 '13 at 16:30
  • so if your download page is something like `echo 'html'; echo file_get_contents('yourfile.txt');` then yeah... you'll get html. the download link code path should contain NO output other than whatever's in your file. – Marc B Mar 11 '13 at 16:35
  • You have to retrieve your desired data from the file or database you want to, you cannot take data just from the html, except if you're doing it with jQuery. – believe me Mar 11 '13 at 16:35
  • so basically your saying there should be no other code on the page to get just the array data into the csv file format? that is indeed a balls – eoin Mar 11 '13 at 16:43
  • could you clarify that for me? cheers – eoin Mar 11 '13 at 16:58
  • http://stackoverflow.com/questions/11995117/fputcsv-inserting-html-code-into-a-csv-file here it is – eoin Mar 11 '13 at 17:38

2 Answers2

1

You can try the below code. You need to add read_file at the end after setting some header as below.

<?php
if(isset($_GET['link']))
{
    $var_1 = $_GET['link'];
    $file = $var_1;

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));
    ob_clean();
    flush();
    readfile($file);
    exit;
    }
} //- the missing closing brace
?>
Aman Garg
  • 3,122
  • 4
  • 24
  • 32
0

use ob_end_clean() ; before writing the file

Khan
  • 151
  • 1
  • 4