Announcement

Collapse
No announcement yet.

fopen() fread() etc

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • fopen() fread() etc

    Can anyone show me some example code to
    [list=1][*]Open a php file on my server[*]Read the contents[*]open a second .txt file[*]write the contents of the first file to the second txt file[/list=1]

    Many thanks
    http://forums.cpfc.org/

  • #2
    do you mean write as in add to that file (eg. hit counter), or write as in just add to that file temporarily (eg. additional coding)

    Comment


    • #3
      PHP Code:
      <?
      $filename 
      "test.txt";
      print 
      "writing to file<br>";
      $fp fopen($filename,"w") or die("Could'nt open $filename");
      fwrite ($fp"Hello\n");
      fclose($fp);
      print 
      "Appending to $filename<br>";
      $fp fopen($filename,"a") or die("error");
      fputs($fp,And another hello\n");
      fclose(
      $fp);
      ?>
      to read a file use fread function

      Comment


      • #4
        Thanks menator but that isn't exactly what I wanted:

        What I want to be able to do is this:

        I have the latest topics from my forums on my homepage, this is called via ssi by a script called active.php

        Now what I want to be able to do is execute via cron active.php every 15 minutes and write the output to a txt file after applying a few simple regex.

        Thanks in advance

        Dave
        http://forums.cpfc.org/

        Comment


        • #5
          Are you using files for this with a reason?
          Cuss it can be done pretty easily with just a few queries... I'm pretty sure.
          Chen Avinadav
          Better to remain silent and be thought a fool than to speak out and remove all doubt.

          גם אני מאוכזב מסיקור תחרות לתור מוטור של NRG הרשת ע"י מעריב

          Comment


          • #6
            No reason for using files - that's just the way I would of done it with PERL I guess
            http://forums.cpfc.org/

            Comment


            • #7
              see if this gives the results your looking for

              PHP Code:
              <?
              $file
              ="test.txt";
              $fp fopen($file,"r");
              while(! 
              feof($fp))
                {
                 
              $chunk fread($fp,16);
                   print 
              "$chunk";
                   }
                   
              $file1="this.txt";
                 
              $fp fopen($file1,"w");
                 
              fwrite($fp,"$chunk\n");
              fclose($fp);

              ?>

              Comment


              • #8
                Thanks - But that didn't work No output

                Here is the code from the script that is running?

                Is there an easy way to make the script write to a .txt file rather than PRINT?

                PHP Code:
                <? 
                require("/home/admin/www/forums/admin/config.php");
                //
                $num_active 10
                $num_chars 75;

                $db=mysql_connect($servername,$dbusername,$dbpassword); 
                mysql_select_db($dbname); 

                $querylatest="SELECT * FROM thread WHERE forumid='1' OR forumid='2' OR
                forumid='3' OR forumid='4' OR forumid='5' OR forumid='6' OR forumid='7' OR
                forumid='8' OR forumid='9' OR forumid='10' OR forumid='11' OR forumid='12'
                OR forumid='13' OR forumid='14' OR forumid='15' OR forumid='16' OR
                forumid='17' OR forumid='18' OR forumid='20' OR forumid='21' OR
                forumid='22' OR forumid='23' OR forumid='24' OR forumid='25' ORDER BY lastpost
                DESC LIMIT 
                $num_active"

                $resultlatest mysql_query($querylatest,$db); 

                while (
                $latest_array mysql_fetch_array($resultlatest)) { 


                // Get Forum Infomation 
                $query_forum "select * from forum where forumid='$latest_array[forumid]'"
                $result_forum mysql_query($query_forum,$db); 
                $forum_info_array mysql_fetch_array($result_forum); 

                // split the date up a bit 
                $datestr1 substr($latest_array["dateline"],0,10); 
                $datetime substr($latest_array["dateline"],11,8); 

                $querythread="SELECT * FROM post WHERE threadid='$latest_array[threadid]' ORDER BY dateline ASC LIMIT 1"

                $result_thread_textmysql_query($querythread,$db); 

                $result_thread_array mysql_fetch_array($result_thread_text); 

                $newstitle $latest_array["title"]; 
                $newsposter $latest_array["postusername"]; 
                $newsposterid $result_thread_array["userid"]; 
                $newsdate date("h:i A"$latest_array["lastpost"]); 
                $newstext substr(strip_tags($result_thread_array["pagetext"]),0,$num_chars);
                $newsthreadid $latest_array["threadid"]; 
                $newscomments $latest_array["replycount"]; 
                $threadforumid $latest_array["forumid"];
                $threadforum $forum_info_array["title"];
                $threadiconid $latest_array["iconid"];

                $newstext strip_tags($newstext,"");
                //$newstext = wordwrap( $newstext, 20 );
                $newstext preg_replace("/[a-zA-Z0-9]{12,}/s"" "$newstext); 
                $newstext ereg_replace("^http"""$newstext);
                $newstext ereg_replace("http://"""$newstext);
                $newstext preg_replace("/\[.*?\]/si",""$newstext);




                if (
                $threadiconid==0) {
                 
                $threadicon "";
                 }
                else {
                 
                $threadicon "<img src=\"http://www.cpfc.org/images/posticon2.gif\" alt=\"Thread icon\"> ";
                 }

                if (
                $newscomments==1) {
                 
                $commenttext "reply »";
                 }
                else {
                 
                $commenttext "replies »";
                 }


                print(
                "
                    
                <b><font color=\"#FF0000\"><font size=\"1\">»</font></font>
                <font size=\"1\">
                 <a href=\"http://www.cpfc.org/forums/showthread.php?threadid=
                $newsthreadid\">$newstitle<br>
                 </a></font></b><font size=\"1\">
                $newstext<BR>$newscomments $commenttext <a href=\"http://www.cpfc.org/forums/showthread.php?threadid=$newsthreadid\">Read
                more</a><BR><font size=\"1\" color=\"#C0C0C0\">Last Updated: 
                $newsdate<br></font>
                "
                ); 


                ?>
                http://forums.cpfc.org/

                Comment


                • #9
                  I'm an idiot

                  I'm stupid

                  I can simply do this

                  /usr/local/php/bin/php /home/admin/www/forums/addons/active3.php > /home/admin/www/forums/addons/test.txt

                  DOH!!
                  http://forums.cpfc.org/

                  Comment

                  widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                  Working...
                  X