script makes a CSV file. how create?

Links for php scripts

Moderators: macek, egami, gesf

mamandis
New php-forum User
New php-forum User
Posts: 10
Joined: Wed Apr 18, 2012 12:36 am

script makes a CSV file. how create?

Postby mamandis » Tue Apr 24, 2012 12:01 am

I have a script which all data from my partners I get prints to a screen (I wrote so) (check it: [url]25h.lt/acc.php[/url])..
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:

User avatar
minimihi
New php-forum User
New php-forum User
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: script makes a CSV file. how create?

Postby minimihi » Tue Apr 24, 2012 11:28 am

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
 

mamandis
New php-forum User
New php-forum User
Posts: 10
Joined: Wed Apr 18, 2012 12:36 am

Re: script makes a CSV file. how create?

Postby mamandis » Tue Apr 24, 2012 11:33 pm

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:

mamandis
New php-forum User
New php-forum User
Posts: 10
Joined: Wed Apr 18, 2012 12:36 am

Re: script makes a CSV file. how create?

Postby mamandis » Wed Apr 25, 2012 11:44 pm

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:

mamandis
New php-forum User
New php-forum User
Posts: 10
Joined: Wed Apr 18, 2012 12:36 am

Re: script makes a CSV file. how create?

Postby mamandis » Thu Apr 26, 2012 2:27 am

yeh.. I see that here is just force download..
Im trying to write sth smarter :?

User avatar
minimihi
New php-forum User
New php-forum User
Posts: 238
Joined: Sat Apr 14, 2012 11:57 am
Location: Vilnius, Lithuania

Re: script makes a CSV file. how create?

Postby minimihi » Sat Apr 28, 2012 12:17 pm

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.


Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 2 guests