创建一个文件夹并插入一个包含代码的index.php文件


create a folder and insert an index.php file with code in it

我正在开发一个cms,并希望用户创建一个文件夹,还插入一个index.php文件与一些行代码要执行。这应该在控制面板上完成,所有的事情都应该使用代码来完成。

感谢

我已经试过这个代码

if (!file_exists('folder_path')) {
    mkdir('folder_path');
}

然而,我不知道我是否可以创建一个index.php文件并插入代码到它使用fopen

$file_name = "index.php";
$create_file = fopen($file_name, "r");

首先,当然可以,有几种方法可以做到这一点。fopen()是其中之一,但我猜您最好尝试使用其中之一:

file_put_contents( $filename, $data ) 

文档链接

或者,根据您对CMS的实现,我猜您可能希望在那里放置一个默认的起始模板,所以只需

copy( $tempalte_file, $new_path )

文档链接

最后,如果您出于某种原因坚持使用fopen,只需使用(w)书写标志,它将尝试创建不存在的文件:

$handle = fopen( $filename, "w" );

文档链接