ページ内のファイルを参照するときに、.txt ファイルの内容を上書きする必要があります。そのための php スクリプトを作成しました。
フォームは以下のとおりです
<input type="file" name="file" size="50" maxlength="25"> <br>
<input type="submit" name="upload" value="Upload">
そしてPHPスクリプトは
$file1=$_FILES['file'];
$out = file_get_contents($file1);
$file2 = "molecule.txt";
$filehandle = fopen($file2, "a");
$file_contents = file($file1);
$count = count(file($file1));
file_put_contents($file2, "");
$newlineno = 0;
foreach ($file_contents as $line_num => $line) {
if (strpos($line, 'Standard orientation:') !== false) {
for ($lineno = $line_num + 5; $lineno <= $count; $lineno++) {
$line = $file_contents[$lineno];
if (strpos($line, 'Rotational constants (GHZ):') != false) {
break;
}
$line = preg_replace('/\s+/', ' ', $line);
$split = explode(" ", $line);
$symbol = $mapping[$split[2]];
$x = $y = $z = '';
if ($split[4] >= 0) {
$x = ' ' . $split[4];
} else {
$x = $split[4];
}
if ($split[5] >= 0) {
$y = ' ' . $split[5];
} else {
$y = $split[5];
}
if ($split[6] >= 0) {
$z = ' ' . $split[6];
} else {
$z = $split[6];
}
$x = substr($x, 0, -3);
$y = substr($y, 0, -3);
$z = substr($z, 0, -3);
$newlineno++;
$newline = "ATOM\t" . $newlineno . "\t" . $x . "\t" . $y . "\t" . $z . "\t" . $symbol . "\n";
fwrite($filehandle, $newline);
}
}
}
fclose($filehandle);
$lines = file($file2);
$last = sizeof($lines) - 1 ;
unset($lines[$last]);
$fp = fopen($file2, 'w');
fwrite($fp, implode('', $lines));
fclose($fp);
echo "File UPLOADED SUCESSFULLLy";
?>
<form method="POST" action="molecules.html"><br><br>
<p><input type="submit" name="structure" value="View file">
ファイルには、ファイルmolecule.html
の内容を表示するためのコードが含まれていますmolecule.txt
ただし、ファイルの内容は上書きされませんmolecules.txt
。
追加する必要があるものはありますか? 私を助けてください。
前もって感謝します。