Leosan Posted September 2, 2009 Share Posted September 2, 2009 I am trying to make by blog software as user friendly as possible, to achieve this I am wanting to make a simple install.php All information which needs to be written to php files has been moved into one file called connect.php My problem is that the fwrite is not functioning correctly. This is the current install file, just the write portion <?php // Variables which will be changed by Installer Input $dbuser='Cole'; $dbpassword='teflon'; $dbmaster = 'main'; $dbslave = 'slave'; $dbsiteid = '1'; $dbprefix = 'blog'; // Config.php Writer $file = "connect.php"; $fh = fopen("connect.php",'r+'); if($fh){ $data = file_get_contents($file); rewind($fh); $data = str_replace("dbuser","$dbuser",$data); $data = str_replace("dbpass","$dbpassword",$data); $data = str_replace("dbnameone","$dbmaster",$data); $data = str_replace("dbnametwo","$dbslave",$data); $data = str_replace("dbsiteid","$dbsiteid",$data); $data = str_replace("dbprefix","$dbprefix",$data); fwrite($fh,$data); fclose($fh); } echo "Writing Completed"; ?> Here is the connect.php which its to write to <?php mysql_connect("localhost", "dbuser", "dbpass"); mysql_select_db("dbnameone"); mysql_select_db("dbnametwo"); $siteid = "dbsiteid"; $prefix = "dbprefix"; $dbmast = "dbnameone"; $dbslave = "dbnametwo"; ?> The result of running the index.php should be <?php mysql_connect("localhost", "Cole", "teflon"); mysql_select_db("main"); mysql_select_db("slave"); $siteid = "1"; $prefix = "blog"; $dbmast = "main"; $dbslave = "slave"; ?> But instead, its writing the file as <?php mysql_connect("localhost", "Cole", "teflon"); mysql_select_db("main"); mysql_select_db("slave"); $siteid = "1"; $prefix = "blog"; $dbmast = "main"; $dbslave = "slave"; ?>; $dbslave = "dbnametwo"; ?> Can anyone help with this, its really boggling my brain. Quote Link to comment https://forums.phpfreaks.com/topic/172868-solved-problem-with-fwrite-function/ Share on other sites More sharing options...
Leosan Posted September 2, 2009 Author Share Posted September 2, 2009 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/172868-solved-problem-with-fwrite-function/#findComment-911122 Share on other sites More sharing options...
Leosan Posted September 3, 2009 Author Share Posted September 3, 2009 Can someone please help with this, its really doing my head in. Quote Link to comment https://forums.phpfreaks.com/topic/172868-solved-problem-with-fwrite-function/#findComment-911873 Share on other sites More sharing options...
Mark Baker Posted September 3, 2009 Share Posted September 3, 2009 use ftruncate() instead of rewind Quote Link to comment https://forums.phpfreaks.com/topic/172868-solved-problem-with-fwrite-function/#findComment-911927 Share on other sites More sharing options...
Leosan Posted September 3, 2009 Author Share Posted September 3, 2009 Thanks, tried it but it still didnt work, its still adding on the extra bit at the end. It did cut off the end bit, but it also did away with the ?> Quote Link to comment https://forums.phpfreaks.com/topic/172868-solved-problem-with-fwrite-function/#findComment-911999 Share on other sites More sharing options...
Leosan Posted September 3, 2009 Author Share Posted September 3, 2009 Cant edit last one for some reason. After checking it a few times, I found that it was a different problem caused, instead of it adding to the end of the file, it has now removed the required ?> at the end. Quote Link to comment https://forums.phpfreaks.com/topic/172868-solved-problem-with-fwrite-function/#findComment-912018 Share on other sites More sharing options...
Leosan Posted September 4, 2009 Author Share Posted September 4, 2009 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/172868-solved-problem-with-fwrite-function/#findComment-912502 Share on other sites More sharing options...
Mark Baker Posted September 4, 2009 Share Posted September 4, 2009 If you're on Windows, try opening the file in r+b mode and using ftruncate() // Config.php Writer $file = "connect.php"; $fh = fopen("connect.php",'r+b'); if($fh){ $data = file_get_contents($file); ftruncate($fh,0); $data = str_replace("dbuser","$dbuser",$data); $data = str_replace("dbpass","$dbpassword",$data); $data = str_replace("dbnameone","$dbmaster",$data); $data = str_replace("dbnametwo","$dbslave",$data); $data = str_replace("dbsiteid","$dbsiteid",$data); $data = str_replace("dbprefix","$dbprefix",$data); fwrite($fh,$data); fclose($fh); } Quote Link to comment https://forums.phpfreaks.com/topic/172868-solved-problem-with-fwrite-function/#findComment-912512 Share on other sites More sharing options...
PFMaBiSmAd Posted September 4, 2009 Share Posted September 4, 2009 Your actual issue is that you are changing the length of the contents in the file and the portion at the end of the existing file when you reduce the length is not being overwritten. You should actually write to a new temporary file (something.tmp), then after you check that the new file has been successfully written, delete the original file, and rename the temp file to the actual name you want it to be (you should do this in any case so that an error won't result in the lose of data.) Quote Link to comment https://forums.phpfreaks.com/topic/172868-solved-problem-with-fwrite-function/#findComment-912520 Share on other sites More sharing options...
Leosan Posted September 4, 2009 Author Share Posted September 4, 2009 Thanks for your help, but its a little late now. I have just finished the whole installer. The way I got around it was to move all data which needed to be written into a seperate config file. From there I had the installer write the exact contents of the to be config file to a .txt file, then went on to rename the file to a php extention and move it to the correct destination. Thanks for you help though, phpfreaks rock, I asked about fwrite on about 10 sites and got no help, is it a no no in coding, or is it that not many people know how to make it work? Quote Link to comment https://forums.phpfreaks.com/topic/172868-solved-problem-with-fwrite-function/#findComment-912700 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.