alkat Posted April 6, 2008 Share Posted April 6, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/99856-write-to-beginning-of-a-file/ Share on other sites More sharing options...
wildteen88 Posted April 6, 2008 Share Posted April 6, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/99856-write-to-beginning-of-a-file/#findComment-510762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.