How to achieve automatic creation of PHP files?
To achieve automatic creation of PHP files, you can use PHP’s built-in file manipulation functions. Here is a simple example code:
<?php
// 文件名
$filename = 'newfile.php';
// 文件内容
$content = '<?php echo "Hello, World!"; ?>';
// 尝试创建文件
if (file_put_contents($filename, $content) !== false) {
echo "文件创建成功!";
} else {
echo "文件创建失败!";
}
?>
In the example above, we defined a file name and its content, then used the function file_put_contents() to create a new PHP file. If the file creation is successful, it will output “File created successfully!”, otherwise it will output “File creation failed!”.
It’s important to ensure that the target folder has write permission for safety reasons, and to appropriately validate and filter both file names and content to prevent malicious code from being written into files.