generate a file on client host

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • proudlyphp
    New Member
    • Jul 2008
    • 16

    generate a file on client host

    Hi all,

    I have a link on my web site that intends to support the user generate a csv file of a table. But I have a problem that it generates the file onto the server - which is not the intention. Please help me to create one.

    The code I have is



    Code:
    $query = "SELECT `Year of Data Entry` ".
    			"INTO OUTFILE '/tmp/files/myfile.csv' " . 
    			"FIELDS TERMINATED BY ',' " .
    			"ENCLOSED BY '\"' " .
    			"LINES TERMINATED BY '\n'	" .
    			"FROM table1";
    
    $result = mysql_query($query) or die(mysql_error());
    
    
    
    Thanks in Advance
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    You have no actual access to your client's hard drive, so your code can not actually put files directly onto your client's computer.

    What you can do is have the server send the file to the client and have them download it.

    For example, you could put this at the bottom of the code you posted:
    [code=php]
    header('content-type: text/csv');
    header('content-disposition: attachment; filename=table. csv');
    readfile('/tmp/files/myfile.csv');
    [/code]
    Which should have your client's browser open a "Save As" dialog.

    Comment

    Working...