tags:

views:

37

answers:

2

Hello all,

I am finding a line in a PHP file using PHP and writing to it by changing a variable at a particular line. Please see function below.

This works fine when I test it on its own. However, when I run it within my main script it doesn't work properly. I find the following sort of thing on = "Version_3_18110"; in the PHP file when it should be $version = "Version_3_18110";

Can this function be effected by echos further up the main script?? The string passed in as $version is always what I need it to be, it just doesn't get written correctly.

What is going on?

function edit_config_version($version){

    $version = trim($version);

    $file = fopen("../includes/db-connect.php", "r") or exit("Unable to open file!");

    $count = 0;

    while(!feof($file)){

            $line = fgets($file);

            if(substr($line, 0, 10)=='$version ='){

                    $line_number = $count;

            }

            $count++;
    }

    fclose($file);

    $count = 0;

    $file = fopen("../includes/db-connect.php", "r+") or exit("Unable to open file!");

    while(!feof($file)){

            if($line_number==$count){

                    fwrite($file, '$version = "Version_'.$version.'";'."\r\n");

            }

            $line = fgets($file);

            $count++;

    }

    fclose($file);

}

Contenets of db_connect.php:

/*
* Date: 06/10/09
* Last Updated: 06/04/2010
*/

$serverName = 'ABS-PC';

$monitor_name = "BTSH_Mon_3_18111";

$version = "Version_3_18112";

$full_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$full_url = explode('view-report.php', $full_url);
$sitePath = $full_url[0];

$full_url = dirname('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']).'/'; 
$sitePathFolder = $full_url;

/* 
* Make sure to close the connection in the scripts
* sqlsrv_close( $conn);
*/
A: 

Have you tried changing:

fwrite($file, '$version = "Version_'.$version.'";'."\r\n");

To

fwrite($file, $version."= \"Version_".$version."\";\r\n");
Ergo Summary
That too doesn't work. It adds a `2= "Version_3_18112";` to that line. No idea where these things are coming from. Btw, I don't have to clear a line before I write to it do I??
Abs
Apologies, try fwrite($file, "\$version= \"Version_".$version."\";\r\n");
Ergo Summary
+1  A: 

Although this is a more exotic approach making use of PHPs tokenizer lib, I think this is better then error-prone string parsing. Try this if it works for you (tested unter PHP 5.3, but should work on any modern PHP version):

<?php
function edit_config_version($version){
    $version = trim($version);

    $source = file_get_contents('config.php');
    $tokens = token_get_all($source);

    $fh = fopen('config.php', 'w');

    foreach ($tokens as $token) {
        if(is_string($token)) {
            fwrite($fh, $token);
            continue;
        }

        list($id, $text) = $token;

        if($id == T_CONSTANT_ENCAPSED_STRING && strpos($text, 'Version_') === 1) fprintf($fh, '"Version_%s"', $version);
        else fwrite($fh, $text);
    }

    fclose($fh);
}

edit_config_version('2345_545454');

Error handling is left as an exercise for the reader. ;-)

Max
Perfect, it has worked, even within the main script which mine failed on. I have no idea why and I have no time investigate as I've already wasted several hours! Thank you for your help Max and I'll be sure to error handle properly. ;)
Abs