Page 1 of 1

script makes a CSV file. how create?

Posted: Tue Apr 24, 2012 12:01 am
by mamandis
I have a script which all data from my partners I get prints to a screen (I wrote so) (check it: 25h.lt/acc.php)..
but now I need write a script (I think few lines in this acc.php file) which will let me download a CSV file.. 8)

I am not proffesional PHP programer and I never wrote a script which make a file.. :oops:


help me please.. :help:

Re: script makes a CSV file. how create?

Posted: Tue Apr 24, 2012 11:28 am
by minimihi
You need to add headers

Code: Select all

header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="filename_user_will_see_when_try_to_download.csv"'); 
and after those headers
mamandis wrote: a script which all data from my partners I get prints to a screen
 

Re: script makes a CSV file. how create?

Posted: Tue Apr 24, 2012 11:33 pm
by mamandis
Thanks for an answer :)
but I get two warnings:
Warning: Cannot modify header information - headers already sent by (output started at /home/hltb/domains/25h.lt/public_html/acc.php:4) in /home/hltb/domains/25h.lt/public_html/acc.php on line 17

-""- line 18

I think this is couse I already sent headers to print on a screen.. am I right?

what to do else? :help:

Re: script makes a CSV file. how create?

Posted: Wed Apr 25, 2012 11:44 pm
by mamandis
I tried to put some lines of code to my code:

Code: Select all

$FileName = date("y-m-d") . '.csv';
function maybeEncodeCSVField($Content) {
    if(strpos($Content, ',') !== false || strpos($Content, '"') !== false || strpos($Content, "\n") !== false) {
        $Content = '"' . str_replace('"', '""', $Content) . '"';
    }
    return $Content;
}


# Titlte of the CSV
$Content = "Name,Address,Age,Phone \n";

# fill data in the CSV
$Content .= $GetProductListRequest;

header('Content-Type: application/csv'); 
header("Content-length: " . filesize($NewFile)); 
header('Content-Disposition: attachment; filename="' . $FileName . '"'); 
echo $Content;
exit();  
and Ive got some lines of warnings-
ex.:
Warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/acc.php:529) in /opt/lampp/htdocs/acc.php on line 793

headers output maybe is showing on an echo/print outputs?

:help:

Re: script makes a CSV file. how create?

Posted: Thu Apr 26, 2012 2:27 am
by mamandis
yeh.. I see that here is just force download..
Im trying to write sth smarter :?

Re: script makes a CSV file. how create?

Posted: Sat Apr 28, 2012 12:17 pm
by minimihi
Force download?

If browser supports file format, it would try to open that file in browser window.
'Force download' means that file won't be opened in browser window in any case, but instead it will be downloaded.

According to above, your use of term 'force download' is weird. There is nothing smarter to think of.
I guess you wanted to say, that file is downloaded automatically (without Open/Save/Cancel dialog window). If that's the case, automatic download is set by browser and not a code.

P.S. Why have you posted a header problem when I answered you via PM? :? As you asked.