I've data inside a MySQL database. I'm delivering the consumer a URL to have their data out like a CSV file.

I've the e-mailing from the link, MySQL query, etc. covered.

How do i, once they follow the link, possess a pop-as much as download a Resumes using the record from MySQL?

I've all the details to find the record already. I simply aren't seeing how you can have PHP produce the CSV file and allow them to download personal files having a .csv extension.


header("Content-type: application/csv")

header("Content-Disposition: attachment filename=file.csv")

header("Pragma: no-cache")

header("Expires: ")

echo "record1,record2,record3n"


header("Content-type: text/csv")

header("Content-Disposition: attachment filename=file.csv")

header("Pragma: no-cache")

header("Expires: ")

$array = array(

    array("data11", "data12", "data13"),

    array("data21", "data22", "data23"),

    array("data31", "data32", "data23"))


function outputCSV($data) amplifier$vals, $key, $filehandler) 

    array_walk($data, "__outputCSV", $outstream)


According to approved answer and http://world wide web.php.internet/manual/en/function.fputcsv.php#100033

Make your file then return a mention of the it using the correct header to trigger the Save As - edit the next when needed. Put your CSV data into $csvdata.

$fname = 'myCSV.csv'

$fp = fopen($fname,'w')



header('Content-type: application/csv')

header("Content-Disposition: inline filename=".$fname)


First make data like a String with comma because the delimiter (separated with ","). Something similar to this

$CSV_string="No,Date,Email,Sender Title,Sender Email n" //making string, So "n" can be used for newLine

$rand = rand(1,50) //Create a random int number between 1 to 50.

$file ="export/export".$rand.".csv" //For staying away from cache within the client as well as on the server

                                     //side it's suggested the file title differ.


/* Or do this code if $CSV_string is definitely an array

    fh =fopen($file, 'w')

    fputcsv($fh , $CSV_string , ","  , "n" ) // "," is delimiter // "n" is totally new line.



Simple method -

$data = array (




$fp = fopen('data.csv', 'w')

foreach($data as $line)


So each type of the $data array will visit a new type of your recently produced CSV file. It only works just for PHP 5 and then.

The simplest way is by using a devoted CSV class such as this:

$csv = new csv()


    array('name'=>'John', 'age'=>35),

    array('name'=>'Adrian', 'age'=>23),

    array('name'=>'William', 'age'=>57)



To be send it as being a CSV and also have it provide the file title, use header():


header('Content-type: text/csv')

header('Content-disposition: attachment filename="myfile.csv"')

So far as making the CSV itself, you'd just loop with the result set, formatting the output and delivering it, exactly like you would every other content.