I have a doubt on file download using php. I have written a script though which i created a report as an array. Now I want the array to be printed an a user defined file name while clicking the link. I created like, the output will be created on the same directory. I don;t want the output to be created in server. I want it to be written on the file of users directory from directly through the array. The creation of the download link also will be displayed on the same page itself.
1
-
1you want to write a new file on the clients machine? i think youll need to create this on server and have them download. if there iweres a way to create the file in the client/browser (which sounds like a stretch to me) it would have to be using javascript.jon_darkstar– jon_darkstar2011-03-25 06:24:19 +00:00Commented Mar 25, 2011 at 6:24
Add a comment
|
1 Answer
You cant control where on the users computer the file is placed.. they control that. You can however let them specify the filename directly through a download form. for example:
Your download link would point to a page with this form or use ajax to pop it up in a modal or something:
<form action="download-report.php">
<label for="filename">Filename</label>
<input type="text" name="filename" />
<div><input type="submit" value="download" />
</form>
In download-report.php
// santize the filename input and then assign it to $filename here
$report = getReportArray(); // or whatever call(s) you need to generate the array
$report = print_r($report, true); // convert the array to a string
header('Content-type: text/plain');
header('Content-length: '.strlen($report));
// this will make it like r-click -> save as, but witht he filename they specified
header('Content-disposition: attachment; filename="'.$filename.'"';
header("HTTP/1.0 200 Ok");
echo $report;
exit(0);
Of course you could just tell them to right-click and save as in an instruction near the link and skip the whole form thing... or just give a general default filename and then let them change it when the dialog pops up...