0

It's possible save a string as text file and save the file on desktop?

I have found only questions about ow to save the file in the server.

I've tried but without luck

user3318546
  • 75
  • 1
  • 7
  • What have you tried? Use [file_set_contents()](http://php.net/file_set_contents). (This won't work of course if you mean the user's desktop.) – ComFreek Mar 03 '14 at 11:04
  • 1
    by desktop do you mean the user's system or the actual desktop folder of a user? because you cannot decide where the user wants to download a file – anurupr Mar 03 '14 at 11:05
  • 1
    Are you asking about server side PHP and the client's desktop? Or are you asking about a PHP application that runs on the same computer as the desktop in question? – Quentin Mar 03 '14 at 11:06
  • 2
    @ComFreek: Did you mean [`file_put_contents`](http://php.net/file_put_contents) :)? – hakre Mar 03 '14 at 11:25
  • Hmm, I see there is an answer accepted! So the desktop is actually on the server, therefore the question answered itself. I doubt there is need to keep this further, see as well [save the file in users desktop](http://stackoverflow.com/questions/986194/save-the-file-in-users-desktop?rq=1) for the keywords. – hakre Mar 03 '14 at 11:28
  • @hakre Oh, right ;) I second the close vote. – ComFreek Mar 03 '14 at 11:31

3 Answers3

2

You cannot force the download location of a file on a users computer, but you can force download a file prompt and have the user choose where to put the file.

This seems to be similar to the following question, where the answer is to 'force download' the file:

$file_url = 'http://www.myremoteserver.com/file.txt';
header('Content-Type: text/plain');
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
readfile($file_url); // do the double-download-dance (dirty but worky)

How to force file download with PHP

Community
  • 1
  • 1
duellsy
  • 8,497
  • 2
  • 36
  • 60
1

//CREATE

$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file); //implicitly creates file

//WRITE

$my_file = 'file.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);
$data = 'This is the data';
fwrite($handle, $data);

DEMO

SagarPPanchal
  • 9,839
  • 6
  • 34
  • 62
0

PHP is server side.

But you could output a .txt file as download. Saving location will be defined by the user.

Check this: http://www.richnetapps.com/the-right-way-to-handle-file-downloads-in-php/

rebyte
  • 70
  • 1
  • 4
  • 3
    Please [avoid link only answers](http://meta.stackoverflow.com/tags/link-only-answers/info). Answers that are "barely more than a link to an external site” [may be deleted](http://stackoverflow.com/help/deleted-answers). – Quentin Mar 03 '14 at 11:10