如何从 html 文件中的 php 代码打开文件

How to open file from php code inside html file

我有“.html”文件,里面有我 运行 php 代码。 php 代码应该打开“.ini”文件。

".ini" file:
blast+: blast+\bin

blastn: test_na_db => Nucleotide test database
blastp: test_aa_db => Protein test database
blastx: test_aa_db => Protein test database
tblastn: test_na_db => Nucleotide test database
tblastx: test_na_db => Nucleotide test database


".html" file:
<td valign=top>&nbsp;&nbsp;&nbsp;Database(s)</td>
<td>
<?php
$fp = fopen ("./filename.ini", "r");
if(!$fp) {
    echo "<p><strong> Error: Couldn't open file filename.ini </strong></p></body></html>";
    exit;
}
while(!feof($fp)) {
    $blastdbstring = rtrim(fgets($fp));
    if (!$blastdbstring) {
        continue;
    }
    if (!preg_match("/^\s*#/", $blastdbstring)) {
        $blastdbArray = preg_split('/:/', $blastdbstring);  
        $blastProgram = $blastdbArray[0];
        $dbString = $blastdbArray[1];

        if ($blastProgram == "blast+") {
            echo "<input type='hidden' name= 'blastpath' value='$dbString'>";
        }else {
            if (preg_match("/^\s*(.*?)\s*$/", $blastProgram, $match)) {
                $blastProgram = $match[1];
            }
            if (preg_match("/^\s*(.*?)(\s*|\s*,\s*)$/", $dbString, $match)) {
                $dbString = $match[1];
            }
            $dbString = preg_replace("/\s*=>\s*/", "=>", $dbString);
            if (preg_match("/,/", $dbString, $match)) {
                $dbString = preg_replace("/\s*,\s*/", ",", $dbString);
            }       
            echo "<input id='$blastProgram' type='hidden' name='blastdb[]' value='$dbString'>";
        }
    }   
}
fclose($fp);

?>
<select id="dbList" size=4 multiple="multiple" name ="patientIDarray[]">
<script type="text/javascript">
    here call js function that create options depend on the selection of previous question
</script>

</select>
</td>

每次我收到此错误消息时: 无法打开文件 filename.ini!!!!

".html"文件和".ini"文件在同一目录下。

我可以完全控制那个“.ini”文件

有什么建议吗?

您可能想使用 parse_ini_file

// Parse without sections
$ini_array = parse_ini_file("sample.ini");
print_r($ini_array);

// Parse with sections
$ini_array = parse_ini_file("sample.ini", true);
print_r($ini_array);

http://php.net/manual/en/function.parse-ini-file.php

请检查文件的权限,可能不是所有人都能访问。

请运行"chmod 666 filename.ini"

试试这个:

$fp = fopen (__DIR__ . "/filename.ini", "r");

查看幻数列表:http://php.net/manual/en/language.constants.predefined.php

只需使用 Ajax 请求获取 INI 文件的内容。

不需要 PHP 为 运行 文件编码。

PHP 脚本现在可以与 .htm 或 .HTML 文件一起使用。