Alex Alex - 3 months ago 11
PHP Question

Creating a php file using php File Write

I tried to create a php file and save it in a root directory but it give error while doing it


Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE),
expecting identifier (T_STRING) or variable (T_VARIABLE) or number
(T_NUM_STRING) in C:\xampp\htdocs\page\cjl\configAlter.php on line 181


here is my code

<?php
$IndexFile = 'default.php';
$IndexCont = "<?php
session_start();
$currentBas = basename(dirname(__FILE__));
$parentBas = basename(dirname(dirname($_SERVER['SCRIPT_NAME'])));//Line 181
$DIR = __DIR__;
require('../../srcc/default.php');
?>";
//This is already define $parentDir."".$SubFolderjailorgname
$fileDirectoryR = $parentDir."".$SubFolderjailorgname;
$myReadfile = fopen($fileDirectoryR.'/'.$IndexFile, "w") or die("Failed to open file!");
fwrite($myReadfile, $IndexCont);
fclose($myReadfile);
//END CREATE TEXT TO FILE
?>

Answer

Using double quotes " means your $_SERVER becomes evaluated in the $IndexCont string.

If you didn't mean it to be evaluated, use single quotes.

If you did need it evaluated, the resulting value in the string would be a syntax error when that new file is loaded, try adding single quotes around it.