0

how can I change only a part of php file. Actually add a variable or change a single variable in already existing php file. I have tried as follows but this erases everything and echoes only the text "I love PHP".

$myFile = '../new_folder_name/index.php'; 
$myContent = 'I love PHP'; 

file_put_contents($myFile, utf8_encode($myContent));

I want to add for example a variable named $test that equals 10. $test="10"; or change already existing variable $test to 10.

5
  • Why do you need to call file_put_contents()? Commented Oct 16, 2013 at 14:48
  • 3
    You read in the file, manipulate its contents in memory, then spit it out again. or read the file line-by-line, find the one line you do want to change, change it, then write out the lines (modified or not) to another file until you're done. then delete the original file, rename the new file, and boom... contents changed. Commented Oct 16, 2013 at 14:48
  • An editor seems a good idea... Commented Oct 16, 2013 at 14:50
  • 2
    you don't want that. dynamic code modification is a hell for debugging and an open door for vulnerabilities Commented Oct 16, 2013 at 14:53
  • 1
    @Dieter: What does AJAX have to do with this question? Did you even read the question? Commented Oct 16, 2013 at 14:53

2 Answers 2

1

I am creating php file with php file

<?php
$dir = "../new_folder_name";

$file_to_write = "index.php";

$content_to_write = '


PHP CODE COMES HERE
$test= GET ('SOMETHING')


';

if( is_dir($dir) === false )
{
mkdir($dir);
}

$file = fopen($dir . '/' . $file_to_write,"w");

fwrite($file, $content_to_write);

fclose($file);

include $dir . '/' . $file_to_write;
?>

I want everyfile to have a different value of a $test variable. I wanted after a file creation to be able to change the value of the variable in the created file

Sign up to request clarification or add additional context in comments.

Comments

0

Try this for adding new content

$myFile = '../new_folder_name/index.php'; 
$myContent = 'I love PHP';
$file=fopen($myFile,"a");
fputs($file,$myContent);
fclose($file);

For playing with variables we need to handle an php file as an normal text file here you go

function get_str_btwn($string, $start, $end)//an function to get string between two string in an string
{
    $string = " " . $string;
    $ini    = strpos($string, $start);
    if ($ini == 0)
        return "";
    $ini += strlen($start);
    $len = strpos($string, $end, $ini) - $ini;
    return substr($string, $ini, $len);
}
$file=file_get_contents($myFile);
if(substr_count($file,"$myContent"))
$file=str_ireplace(get_str_btwn($file,"$myContent =",";"),"I love PHP",$file);//Assuming format of php file is $myContent is initialized like $myContent="something"; 
else
$file=str_ireplace("<?php","<?php $myContent = 'I love PHP';",$file);

Please note this code is very sensitive and may result in errors in php file generated and is not recommended for regular use unless you are sure format of php file.

2 Comments

that is the one, how can I now play with the variables. How could I in $mycontent add a $test variable or a new value for a test variable
the only thing is that the code appears at the bottom of the changing code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.