如何使用php创建.php文件?

Moh*_*aid 9 php

我要做的是创建一个安装文件,用户输入数据库,用户名,密码和主机作为php系统安装的第一步.

Sha*_*ngh 15

它与您创建其他文件相同,但只需添加扩展名 .php

$fp=fopen('filename.php','w');
fwrite($fp, 'data to be written');
fclose($fp);
Run Code Online (Sandbox Code Playgroud)


KAR*_*ván 10

这很简单.只需像其他人提到的那样用php扩展名编写一个文件

但我宁愿为配置数据编写一个ini文件,稍后再加载它们parse_ini_file.

更新:这是一个例子:

<?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"));
Run Code Online (Sandbox Code Playgroud)