Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/172868-solved-problem-with-fwrite-function/
Share on other sites

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);
}

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.)

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.