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 11 of 11
  1. #1
    New Coder
    Join Date
    Mar 2010
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post

    Send data as file

    I´m trying to send a user a page, where I want to send part of the page as a file, like an attachment to an e-mail.

    Something like

    Code:
    $query = mysql_query("SELECT * FROM `someData`");
    while($row = mysql_fetch_object($query)) {
    if($SomeCondition) {
    print $row->data;
    } else {
    $putinfile = $putinfile.$row->data;
    }
    }
    
    // here I want to send $putinfile as a file to the user
    I have no clue how I could do that, but I believe there should be a way other than fwrite() it into a file and print a link into the browser?

    I don´t mind if the user gets the normal download dialog displayed, where he can choose where he wants to save the file, all I need is a way to send data as file, without having an actual file on the server.

    Could anyone give me a hint?
    Thanks in advance.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    What is the filetype?
    Is $putinfile a filename or some text ... or what is it?

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Untested but should do the trick:
    PHP Code:
    header'Content-type: application/octet-stream' );
    header'Content-length: ' strlen$putinfile ) );
    header'Content-disposition: attachment; filename="whateveryouwant.txt"' );

    echo 
    $putinfile;

    exit; 
    Or, using PHP's output stream (just for good measure):
    PHP Code:
    header'Content-type: application/octet-stream' );
    header'Content-length: ' strlen$putinfile ) );
    header'Content-disposition: attachment; filename="whateveryouwant.txt"' );

    if ( 
    FALSE !== ( $fp fopen'php://output''w' ) ) )
    {
        
    fwrite$fp$putinfile );
        
    fclose();
    }

    exit; 
    The output streaming would be good if you potentially have a LOT of row data to output, and instead of gobbing up a ton of memory in one fell swoop, you could stream it to the client as it is retrieved.
    Last edited by kbluhm; 03-11-2010 at 11:46 PM.

  • Users who have thanked kbluhm for this post:

    Goldfish (03-11-2010)

  • #4
    New Coder
    Join Date
    Mar 2010
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    I have to try that.
    Thanks

    Edit:
    The good news: It works, the user receives the file.
    The bad news: The browser doesn´t print the rest of the page, but remains on the previous page.

    So how can I print the page the browser should display after the file has been sent, without the user has to click somewhere?

    Code:
    $temp = "xyz";
    header( 'Content-type: application/octet-stream' );
    header( 'Content-length: ' . strlen( $temp ) );
    header( 'Content-disposition: attachment; filename="whatever.txt"' );
    echo $temp;  
    
    print $RestofPage
    Last edited by Goldfish; 03-11-2010 at 11:56 PM.

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Can you show us the URL so we can see how it flows?

  • #6
    New Coder
    Join Date
    Mar 2010
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    There is no URL (yet)

    All I do so far is checking out how it would work, before I wrap a complete script around it, meaning I´m reducing it to the core function, which in this case is, send a file plus print a page.

    You may imagine an empty Website, where the index.php contains nothing but this:

    Code:
    <?php
    $temp = "xyz";
    header( 'Content-type: application/octet-stream' );
    header( 'Content-length: ' . strlen( $temp ) );
    header( 'Content-disposition: attachment; filename="whatever.txt"' );
    echo $temp;
    ?>
    <html>
    <head><title>Test</title></head>
    <body>
    <p>
    Test
    </p>
    </body></html>
    This will successfully send a file named "whatever.txt" with the content "xyz" to the user, but unfortunately it won´t write the HTML part to the users browser.

  • #7
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    No, the content must be streamed from it's own file in this case, with no output afterward. I alluded to that by placing exit() at the end of each example.

  • Users who have thanked kbluhm for this post:

    Goldfish (03-12-2010)

  • #8
    New Coder
    Join Date
    Mar 2010
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    Ah well, I figured that by now, so I guess I have to work around it.
    Thanks anyway, very fast, very helpful.

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,538
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    So what's the purpose of "the attachment".
    Is there a reason the output couldn't be displayed on the page that is being displayed?
    So instead of the header, you have a PHP "include" on the page they are viewing?

    I can't figure-out what the purpose is ... what you're doing.

  • #10
    New Coder
    Join Date
    Mar 2010
    Posts
    26
    Thanks
    3
    Thanked 1 Time in 1 Post
    The purpose is a complex calculation of tons of numbers, where I want to display a summary of the result in the users browser, while at same time sending him a detailed version as file.
    The reason why I don´t display both in the browser is, the file can easily reach 50 pages of text, while the summary fits into 20 lines.

    Side note: Thanks again to kbluhm for the streaming version, I really can use that one.

    As an example, imagine you want to create statistics about visitors on your homepage.

    The summary would show:
    The site has x members.
    In last x days y users have logged in.
    The most users online were x at date/time.

    The detailed file then shows something like:

    User name1 login date/time
    clicked x pages
    stayed y minutes
    caused z KB traffic

    User name 2 login date/time

    etc etc

  • #11
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    No problem, make sure you place the fwrite() within your result-fetching while-loop, like so:
    PHP Code:
    $query mysql_query'SELECT `data` FROM `table`' );

    if ( 
    FALSE !== ( $fp fopen'php://output''w' ) ) )
    {

        while ( 
    $row mysql_fetch_object$query ) )
        {
            
    fwrite$fp$row->data );
        }

        
    fclose();

    }

    mysql_free_result$query ); 
    Also, with this method you will be unable to accurately set the Content-length header... so just leave it out and keep the client on it's toes over how much downloading is left to go.
    Last edited by kbluhm; 03-12-2010 at 05:30 AM.

  • Users who have thanked kbluhm for this post:

    Goldfish (03-12-2010)


  •  

    Posting Permissions

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