Jump to content

Write to beginning of a file


alkat

Recommended Posts

Hello,

last week some guy here helped me write a script but now I have another issue for which I need helping.

 

There's something wrong with the following script and how it writes text to the beginning of a file:

 

<?php
$dir = "./files/";

/*$find["/*"] = "<noedit>/*";*/
$find["\" = \""] = "\" = \"</NOEDIT><TRAD>";
$find["\"=\""] = "\"=\"</NOEDIT><TRAD>";
$find["\";"] = "</TRAD><NOEDIT>\";";

// Open a known directory, and proceed to read its contents
if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($filename = readdir($dh)) !== false) {
            if(preg_match('/\.strings$/sm', $filename )) // check if the file ends with .strings
            {
               $file = $dir."/".$filename;
               $str = file_get_contents($file); // reads entire file into a string
               $str = str_replace(array_keys($find), array_values($find), $str ); // 
               file_put_contents($file.".html",$str); // adds .html to the filename
       // opens file to write to it
               $handle1 = fopen($file.".html", 'r+'); 
               $opentags = "<OPEN><NOEDIT>\n"; 
               fwrite($handle1, $opentags); // adds opening tags to the beginning of the file
       // opens file to write to it
               $handle2 = fopen($file.".html", 'a'); 
               $closetags = "</NOEDIT></OPEN>"; 
               fwrite($handle2, $closetags); // adds closing tags to the end of the file
            }
        }
        closedir($dh);
    }
}
?>

 

When writing to the beginning of the file, it deletes part of the first line of the original file. Here's the original:

 

/* Tue Apr 01 14:55:39 +0200 2008 --- file: pl-Program.strings --- encoding utf-16 be */

/* begin: $SRCROOT//Option/GlobeTrotter Connect/Resources/pl.lproj/Localizable.strings */

"Change network mode error" = "Change network mode error";

 

And the same file after running my script against it:

 

<OPEN><NOEDIT>
r 01 14:55:39 +0200 2008 --- file: pl-Program.strings --- encoding utf-16 be */

/* begin: $SRCROOT//Option/GlobeTrotter Connect/Resources/pl.lproj/Localizable.strings */

"Change network mode error" = "</NOEDIT><TRAD>Change network mode error</TRAD><NOEDIT>";

 

As you can see, the first part of the text is cut off. The same does not happen with the end of the file.

 

Can anyone shed some light on this issue? :)

 

Thanks,

Ale.

Link to comment
https://forums.phpfreaks.com/topic/99856-write-to-beginning-of-a-file/
Share on other sites

try:

<?php

$dir = "./files/";

/*$find["/*"] = "<noedit>/*";*/
$find["\" = \""] = "\" = \"</NOEDIT><TRAD>";
$find["\"=\""] = "\"=\"</NOEDIT><TRAD>";
$find["\";"] = "</TRAD><NOEDIT>\";";

// Open a known directory, and proceed to read its contents
if (is_dir($dir))
{
    if ($dh = opendir($dir))
    {
        while (($filename = readdir($dh)) !== false)
        {
            if(preg_match('/\.strings$/sm', $filename )) // check if the file ends with .strings
            {
                $file = $dir . '/' . $filename;

                $str = file_get_contents($file); // reads entire file into a string
                $str = str_replace(array_keys($find), array_values($find), $str );

                $str = "<OPEN><NOEDIT>\n" . $str . "\n</NOEDIT></OPEN>";

               file_put_contents($file.'.html', $str); // adds .html to the filename
            }
        }

        closedir($dh);
    }
}

?>

 

I remove the need to have to following lines:

	       // opens file to write to it
               $handle1 = fopen($file.".html", 'r+'); 
               $opentags = "<OPEN><NOEDIT>\n"; 
               fwrite($handle1, $opentags); // adds opening tags to the beginning of the file
       // opens file to write to it
               $handle2 = fopen($file.".html", 'a'); 
               $closetags = "</NOEDIT></OPEN>"; 
               fwrite($handle2, $closetags); // adds closing tags to the end of the file

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.