Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Saving text file to users desktop

    Hi all.
    I'd like to have a script that would save a text file to the users desktop (or any location they request if that not too complexed).
    Code:
    $filename ="text.txt";
    $myFile= fopen($filename,'a');
    $string="This is what is saved";
    fputs($myfile, $string);
    $fclose($myfile);
    The code above will write to a file that 'already' exsits on the server but I want to generate the file from scratch and have it save on the users computer. I'll be using data from mysql db. Any help would be appreciated.

    Cheers
    Steve

  • #2
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    Here is a simple example of how you can achieve this

    PHP Code:
    <?php
    // file to save is of type Microsoft Word document 
    header('Content-type: application/doc);

    // here you can specify the name of the file...you can call it
    // anything you want...
    header('
    Content-Dispositionattachmentfilename="file_name.doc"');

    // now start reading the file on your server to start 
    // downloading to user'
    s desktop
    readfile
    ('documents/original_document.doc');
    ?>
    hope that helps...

    good luck
    ess

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    3
    Thanked 0 Times in 0 Posts
    That's brilliant.

    Just what I needed.

    Cheers
    Steve

  • #4
    New Coder
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Have a problem with the supplied code. Let me explain. The file I generate on the server is pure ASCII but unfortunately when the header download script run it does download a text file but wraps it in HTML (I didn't see this at first as the text editor I was using hid the HTML). The thing is the ASCII file on the server contains Quark Xpress Tag information which looks like HTML code ie <z7f"Arial"> This means that when I try to import the text file generated by the header script it would not import into Quark incorrectly as <body> is not a Quark Tag. Does that make sense? Basically I need to be able to simply download the ASCII text file without making any changes to the file itself. Any ideas.

    Cheers
    Steve

  • #5
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    Hi Steve

    try changing this line

    header('Content-type: application/doc');


    to the following

    header('Content-type: application/txt');

    and while you at it....try adding the following at the end of the script.

    // this ensures the response is terminated once download is complete.
    exit();

    I hope that helps.
    ess

  • #6
    New Coder
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Hi ESS

    I originally changed the doc type but have also now tried with the exit(); which makes no difference. It still wraps the original text file in html code. To be fair things are getting more confusing as I go on. The original output with the (unseen) html looked right I need the plain ascii text file to look like this:
    <z6f"Arial Black">SONY<f"Arial"> Playstation in good condition.
    <z6f"Arial Black">SONY<f"Arial"> Playstation in good condition.

    So using your header I had to output this string like so:
    $string = "&lt;z8fAriel Black&gt;xxxxxxxxxxxxxx&lt;fAriel&gt;xxxxxxxxxxxxx <br> \n";
    The text in the downloaded text file did view correctly in the text editor (as above) I was using but importing it as ascii text into quark showed that not only was it wrapped in html but each line looked like the above string with the &lt and not the < symbol.

    It appears that in the text editor I can get the text to 'look' how I want it but the file is not really composed how it looks.

    Sorry if that doesn't make much sense but it doesn't to me either!!

    Steve

  • #7
    New Coder
    Join Date
    Jul 2006
    Posts
    96
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Sorry but I'm so stupid.

    I placed it at the bottom of my html file.. Hence grabbing all the html tags.

    Saved as a file on it's own, just as php, and it works.

    I have the dunce hat on and am sat in the corner! LOL

    Steve

  • #8
    ess
    ess is offline
    Regular Coder
    Join Date
    Oct 2006
    Location
    United Kingdom
    Posts
    866
    Thanks
    7
    Thanked 30 Times in 29 Posts
    Hi Steve

    Well...I am glad that it worked in the end. I wasn't sure why it didn't work for you.

    I ran a couple of tests on my local server on windows xp and anther on Fedora 6...(I use virtual machines for running testing...as it gives me a chance to test my code in every possible situation)...and wasn't sure why it didn't work for you.

    Anyway..glad that all is well in the end.

    cheers,
    ess


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •