a little function/fwrite help...

Discussion in 'PHP' started by Funk-woo10, Jun 22, 2009.

  1. #1
    Ok,

    I have a function that pulls info out of DB,

    I want this info to be written to a textfile.

    Code looks like this..

    
    
    $myFile = "../sitemap.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = 'displayInfo()';
    fwrite($fh, $stringData);
    fclose($fh);
    
    PHP:

    There is of course an error, $stringdata needs to contain the output from the function or a variable with the function info in...


    My eyes hurt..reps going for help !
     
    Funk-woo10, Jun 22, 2009 IP
  2. franklyn

    franklyn Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    is displayinfo a function in the file ?.
     
    franklyn, Jun 22, 2009 IP
  3. TeamRadiance

    TeamRadiance Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Take the single quotes off the function name. Like this:
    $stringData = displayInfo();
     
    TeamRadiance, Jun 22, 2009 IP
  4. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #4
    $stringData = 'displayInfo()'; makes it constant.

    Correction : $stringData = displayInfo();

    Thanks :)
     
    harrisunderwork, Jun 22, 2009 IP
  5. franklyn

    franklyn Peon

    Messages:
    26
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    no problem.
     
    franklyn, Jun 22, 2009 IP
  6. Funk-woo10

    Funk-woo10 Peon

    Messages:
    1,108
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #6
    thanks fot the help, this still does not print the data into the text file. Just a blank t.xt file !

    $myFile = "../sitemap.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = displayInfo();
    fwrite($fh, $stringData);
    fclose($fh);
     
    Funk-woo10, Jun 23, 2009 IP
  7. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #7
    Try this :

    echo displayInfo;

    What is output ?
     
    harrisunderwork, Jun 23, 2009 IP
  8. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #8
    Kaizoku, Jun 23, 2009 IP
  9. Funk-woo10

    Funk-woo10 Peon

    Messages:
    1,108
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #9
    so it would ook like this -

    echo file_put_contents("../sitemap.txt","displayUsers()");
     
    Funk-woo10, Jun 23, 2009 IP
  10. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #10
    No.. I suggest you learn php from the basics..
     
    Kaizoku, Jun 23, 2009 IP
  11. harrisunderwork

    harrisunderwork Well-Known Member

    Messages:
    1,005
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    135
    #11
    echo displayInfo();

    Try this man.
     
    harrisunderwork, Jun 23, 2009 IP
  12. Funk-woo10

    Funk-woo10 Peon

    Messages:
    1,108
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #12

    that just echos out the results to the page...
     
    Funk-woo10, Jun 23, 2009 IP
  13. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Why do you have an echo here?

    Anyway, you need to get the contents (file_get_contents) and then append the new data to the contents then put the contents (file_put_contents).

    so somethining like:
    
    <?php
    $myFile = "../sitemap.txt";
    
    //get contents
    $fh = file_get_contents($myFile);
    
    //append
    $stringData = displayInfo();
    $fh .= $stringData;
    
    //put
    file_put_contents($myFile, $fh);
    ?>
    
    Code (markup):
    But the other method might be better i'm just trying to explain this method
     
    wd_2k6, Jun 23, 2009 IP
  14. Funk-woo10

    Funk-woo10 Peon

    Messages:
    1,108
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #14
    thanks for your help, adopting wd_2k6 's method still prints nothing to the text file. Odd as it looks as it should..
     
    Funk-woo10, Jun 23, 2009 IP
  15. www.amagit.com

    www.amagit.com Peon

    Messages:
    87
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Do a test to see if displayUsers() is actually returning a value. If it isn't then nothing will be added to the text file.
     
    www.amagit.com, Jun 23, 2009 IP
  16. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #16
    File might be readonly but ya it must produce error / warning.


    Following must put contents to sitemap.txt, am not sure if you php distribution has this function.

    file_put_contents( "../sitemap.txt", displayInfo() );
    PHP:

    IMPORTANT

    Make sure displayInfo() returns something. If it just displays as its name is, then it means it is not returning anything and file contents will be empty. In such case use following:

    <?
    
    	ob_start();
    
    	displayInfo(); //output to be grabbed
    	
    	$data = ob_get_contents(); //store output to a variable
    	
    	ob_end_clean();
    
    	file_put_contents( "../sitemap.txt", $data );
    
    ?>
    PHP:
    regards
     
    Vooler, Jun 23, 2009 IP
  17. Kaizoku

    Kaizoku Well-Known Member

    Messages:
    1,261
    Likes Received:
    20
    Best Answers:
    1
    Trophy Points:
    105
    #17
    No you don't, you can use the FILE_APPEND flag.
     
    Kaizoku, Jun 23, 2009 IP