如何使用 php 创建 .php 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6411656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create .php files using php?
提问by Mohamed Said
What I'm trying to do is to make an installation file where the user enters the database, username, password, and host as a first step in a php system installation.
我想要做的是制作一个安装文件,用户在其中输入数据库、用户名、密码和主机作为 php 系统安装的第一步。
回答by Shakti Singh
It is same as you are creating other files but just add the extension .php
它与您创建其他文件相同,但只需添加扩展名 .php
$fp=fopen('filename.php','w');
fwrite($fp, 'data to be written');
fclose($fp);
回答by KARASZI István
It's easy. Simply write a file with php extension as others mentioned.
这很简单。就像其他人提到的那样,只需编写一个带有 php 扩展名的文件。
But I would rather write an ini file for configuration data and load them later with parse_ini_file
.
但我宁愿为配置数据编写一个 ini 文件,然后用parse_ini_file
.
Update:Here is an example to do that:
更新:这是一个例子:
<?php
$config = array(
"database" => "test",
"user" => "testUser"
);
function writeConfig( $filename, $config ) {
$fh = fopen($filename, "w");
if (!is_resource($fh)) {
return false;
}
foreach ($config as $key => $value) {
fwrite($fh, sprintf("%s = %s\n", $key, $value));
}
fclose($fh);
return true;
}
function readConfig( $filename ) {
return parse_ini_file($filename, false, INI_SCANNER_NORMAL);
}
var_dump(writeConfig("test.ini", $config));
var_dump(readConfig("test.ini"));