PHP Str_Replace & Fwrite questions...

Discussion in 'PHP' started by LazyD, Oct 29, 2006.

  1. #1
    Hello Everyone, im currently working on a site and im having an issue. Let me give you a quick background on the process:

    There are 2 files - Basetemplate.php and Createfile.php

    Createfile.Php is executed-
    On a while loop it reads City Names and for each, copies Basetemplate, opens it, reads the file line by line, replaces certain keywords like "CITYHERE" and puts in the City Name, finally, it closes the file and renames it to another folder with the City Name.

    Everything is working peachy except for a tiny little thing. On about 50% of the outputted files that the script creates it randomly adds a few extra lines from the end of the original Basetemplate.php and sticks it on the end causing some random lines of code to popup below the footer.

    Currently my host is down, but as soon as it comes back up I will post the Createfile.php code....Until then, if anyone has any ideas...
     
    LazyD, Oct 29, 2006 IP
  2. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well try to clean the buffer used to read the files - in the beginning of the while put $buffer='';
    That's as much as I can say for now but if I see some code I might be able to help more.
     
    maiahost, Oct 29, 2006 IP
  3. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I've had a similar problem recently (although only a single space character) which I solved just with the trim() function. Simple but it worked for my situation.
     
    streety, Oct 29, 2006 IP
  4. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    <?php
    include("../Admin/config.php");
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM county";
    $result=mysql_query($query);
    $num=mysql_numrows($result);
    
    mysql_close();
    
    $i = 0;
    $replacestr = "COUNTYHERE";
    while ($i < $num)
    {
            $id = mysql_result($result, $i, "id");
            $county = mysql_result($result, $i, "county");
            $output = str_replace(" ","_","$county");
            
           
    				copy ("basetemplate.php", "$output.php");
    				echo "$output Created";
    				echo "<BR>";
    				$ourFileName = "$output.php";
    				$ourFileHandle = fopen($ourFileName, 'r+') or die("can't open file");
    				$FileData = file_get_contents($ourFileName);
    				$FileDataReplace = str_replace($replacestr,$county, $FileData);
    				fwrite($ourFileHandle, $FileDataReplace);
    				fclose($ourFileHandle);
    				rename ("$output.php", "../county/$output.php") or die ("Could not move file");
    				echo "$output Moved";
    				echo "<BR>";
    				echo "<BR>";
    				
            $i++;
          }
    						
    ?>
    
    Code (markup):
     
    LazyD, Oct 30, 2006 IP
  5. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #5
    not exactly sure if I'd use file_get_contents but try to put $FileData = ''; at the beginning of the loop
     
    maiahost, Oct 30, 2006 IP
  6. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    No Luck, still does the same thing, I was thinking of using fgets or something but im not sure how to rig that up...
     
    LazyD, Oct 30, 2006 IP
  7. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #7
    OK let me see if I can post some code
    replace
    
    $ourFileHandle = fopen($ourFileName, 'r+') 
    or die("can't open file");
    $FileData = file_get_contents($ourFileName);
    				
    
    Code (markup):
    with

    
    $FileData = '';
    $ourFileHandle = fopen($ourFileName, "r" ) ;		
    		   while (!feof($ourFileHandle)) 
    		   {
    			  $FileData .=fgets($ourFileHandle, 10000);
    			}
    
    Code (markup):
     
    maiahost, Oct 30, 2006 IP
  8. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Well it works fine now, it doesnt have the extra stuff at the bottom, but the str_replace isnt working now...

    http://calrealty.us/county/Kings.php

    See, the Keyword isnt being replaced...
     
    LazyD, Oct 30, 2006 IP
  9. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #9
    OK I know why you have that problem.
    You are reading and writing the same file. The code however confuses me a bit. Let me see :
    Just put another file name like :
    replace:
    
    fwrite($ourFileHandle, $FileDataReplace);
    fclose($ourFileHandle);
    rename ("$output.php", "../county/$output.php") or die ("Could not move file");
    				
    
    Code (markup):
    with
    
    $ourFileName2 = "$output2.php";
    $ourFileHandle2 = fopen($ourFileName2, 'w+') or die("can't open file");
    fwrite($ourFileHandle2, $FileDataReplace);
    fclose($ourFileHandle2);
    fclose($ourFileHandle);
    rename ("$output2.php", "../county/$output.php") or die ("Could not move file");
    				
    
    Code (markup):
     
    maiahost, Oct 30, 2006 IP
    LazyD likes this.
  10. LazyD

    LazyD Peon

    Messages:
    425
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Awesome, works like a charm now, thank you so much..
     
    LazyD, Oct 30, 2006 IP
  11. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Not really sure if it will do what it's supposed to but ... no problem :)
     
    maiahost, Oct 30, 2006 IP