Php code to create php file and write php code into that file

Make sure file a.php should be writable

<?php
$filename = 'a.php';
$phpcode = "<?php
\$arr[] = array('2','3','4');
\$var2 = 5;
echo \$var=\$var2;
print_r(\$arr);
";
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $phpcode) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>

Share This: