scubes13 Posted January 2, 2008 Share Posted January 2, 2008 Hello everyone, I have several directories filled with .php files. I am trying to write a script that will take a string of text and insert this text at the top of each .php file while leaving the remainder of the existing code intact. Here is what I have thus far: <?php $file_name = "test.php"; if(file_exists($file_name)) { // Open file for writng and place pointer at the end $handle = fopen($file_name, 'r+'); if(!$handle) { die("Couldn't open file <i>$file_name</i>"); } //place pointer at the beginning of the file. //rewind($handle); // Echo the existing text for debugging $orig_string = fread($handle, filesize($file_name)); echo $orig_string; fseek($handle, 0); $string = "<?php\n"; $string .= "define('QUADODO_IN_SYSTEM', true);\n"; $string .= "require_once('includes/header.php');\n"; $string .= '$qls'."->Security->check_auth_page('"; $string .= $file_name; $string .= "');\n"; $string .= '?'.">\n"; //write to file fwrite($handle, $string); echo "Success writing to file."; } else { echo "File <i>$file_name</i> doesn't exist."; } fclose($handle); ?> My test.php file looks like this: This is a line of text 1 This is a line of text 2 This is a line of text 3 This is a line of text 4 This is a line of text 5 This is a line of text 6 This is a line of text 7 This is a line of text 8 This is a line of text 9 This is a line of text 10 This is a line of text 11 This is a line of text 12 This is a line of text 13 This is a line of text 14 This is a line of text 15 Once I run the above code, I get this: <?php define('QUADODO_IN_SYSTEM', true); require_once('includes/header.php'); $qls->Security->check_auth_page('test.php'); ?> his is a line of text 6 This is a line of text 7 This is a line of text 8 This is a line of text 9 This is a line of text 10 This is a line of text 11 This is a line of text 12 This is a line of text 13 This is a line of text 14 This is a line of text 15 You will notice that it does put the code at the top of the page, but it writes over several lines. Any ideas as to what I am doing incorrectly? Thanks, Kevin L. Quote Link to comment Share on other sites More sharing options...
Daukan Posted January 2, 2008 Share Posted January 2, 2008 You need to save the file contents into a variable. Add the old content to the end of the new content, then write it to the file. fwrite will alway overwrite the same amount of bytes it as the new content. The pointer is at the beginning but it doesn't 'push' the old content down it just overwrites it. Quote Link to comment Share on other sites More sharing options...
scubes13 Posted January 2, 2008 Author Share Posted January 2, 2008 Ok, that makes sense. Any suggestions as how to grab the existing php content without dorking up the formatting? At the moment all of my output is getting shoved onto one continuous line when I try to copy the existing php code from the file. Thanks so much! Kevin L. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted January 2, 2008 Share Posted January 2, 2008 If you're using PHP5, you can used the functions file_get_contents() and file_put_contents(). <?php $file_name = "test.php"; if(file_exists($file_name)) { // get the contents of the file $orig = file_get_contents($file_name); $string = "<?php\n"; $string .= "define('QUADODO_IN_SYSTEM', true);\n"; $string .= "require_once('includes/header.php');\n"; $string .= '$qls'."->Security->check_auth_page('"; $string .= $file_name; $string .= "');\n"; $string .= '?'.">\n"; $string .= $orig; // append the original contents onto the new string. //write to file file_put_contents($file_name,$string); echo "Success writing to file."; } else { echo "File <i>$file_name</i> doesn't exist."; } ?> Ken Quote Link to comment Share on other sites More sharing options...
scubes13 Posted January 2, 2008 Author Share Posted January 2, 2008 Thanks much. That worked like a charm. I am reading up on file_get_contents and file_put_contents now. Thanks again! Kevin L. Quote Link to comment Share on other sites More sharing options...
kishan Posted January 2, 2008 Share Posted January 2, 2008 define('QUADODO_IN_SYSTEM', true); require_once('includes/header.php'); $qls->Security->check_auth_page('test.php'); if in every page ur appending this common code where as the param has to be get dynamically u can write this code in a php file and include it in each page but based on the url or Querystring we have to pass the parameter ... Quote Link to comment 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.