当我浏览页面中的文件时,我需要覆盖 .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
文件的内容。
我还需要补充什么吗?请帮我。
提前致谢。