在php中覆盖.txt文件的内容
我需要在浏览页面中的文件时覆盖.txt文件的内容。我已经为此编写了一个php脚本 表格如下在php中覆盖.txt文件的内容,php,file,file-put-contents,Php,File,File Put Contents,我需要在浏览页面中的文件时覆盖.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); $fil
<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">
$file1=$\u文件['file'];
$out=文件内容($file1);
$file2=“molecular.txt”;
$filehandle=fopen($file2,“a”);
$file\u contents=文件($file1);
$count=count(文件($file1));
文件内容($file2,“”);
$newlineno=0;
foreach($line\u文件内容为$line\u num=>$line){
if(strpos($line,'Standard orientation:')!==false){
对于($lineno=$line_num+5;$lineno=0){
$x=''.$split[4];
}否则{
$x=$split[4];
}
如果($split[5]>=0){
$y=''.$split[5];
}否则{
$y=$split[5];
}
如果($split[6]>=0){
$z=''.$split[6];
}否则{
$z=$split[6];
}
$x=子序列($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=文件($file2);
$last=sizeof($lines)-1;
未设置($line[$last]);
$fp=fopen($file2,'w');
fwrite($fp,内爆(“”,$line));
fclose($fp);
echo“文件上传成功”;
?>
在molecular.html
文件中,包含用于显示molecular.txt
文件内容的代码
但它不会覆盖molecles.txt
文件的内容
我还需要补充什么吗?请帮帮我
提前谢谢。代替
$filehandle = fopen($file2, "a"); // this mode means append content
使用
//w+:开放阅读和写作;将文件指针放在文件的开头,并将文件截断为零长度。如果文件不存在,请尝试创建它
参考:
注意-在“w”模式下使用fopen不会像您预期的那样更新文件的修改时间(filemtime)。您可能希望在写入并关闭文件后发出touch(),以更新其修改时间。在缓存情况下,这可能变得至关重要
我建议采取两种方法
方法-1(文件处理,最简单的方法;考虑到您没有执行任何逻辑操作或读取内容中的字符串匹配)
molecles.html-包含要上载文件的表单的页面
<form method="POST" action="fileOverwrite.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" name="upload" value="Upload" />
</form>
<form method="POST" action="test.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" name="upload" value="Upload" />
</form>
fileOverwrite.php-用于覆盖逻辑/脚本的文件
<?php
$file1 = $_FILES['file']['tmp_name'];
$out = file_get_contents($file1);
$file2 = "molecule.txt";
$filehandle= fopen($file2, "w+") or die("Unable to open file!");
fwrite($filehandle, $out);
fclose($filehandle);
echo "File UPLOADED SUCESSFULLLY";
?>
<form method="POST" action="molecules.html">
<br/ ><br />
<input type="submit" name="structure" value="View file" />
</form>
<?php
// Capture the file's temp path and name
$file1 = $_FILES['file']['tmp_name'];
// Reads entire file into a string
$out = file_get_contents( $file1 );
// Target file
$file2 = "molecule.txt";
// Open file for reading and writing
$filehandle = fopen( $file2, "w+" );
//Reads entire file into an array
$file_contents = file( $file1 );
$count = count( file( $file1 ) );
// Write a string to a file
file_put_contents( $file2, $file_contents );
$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";
// Binary-safe file write
fwrite($filehandle, $newline);
}
}
}
// Close open file pointer
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 />
<input type="submit" name="structure" value="View file" />
</form>
方法-2(符合您的要求,您正在进行字符串搜索)
molecles.html-包含要上载文件的表单的页面
<form method="POST" action="fileOverwrite.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" name="upload" value="Upload" />
</form>
<form method="POST" action="test.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" />
<br />
<input type="submit" name="upload" value="Upload" />
</form>
fileOverwrite.php-用于覆盖逻辑/脚本的文件
<?php
$file1 = $_FILES['file']['tmp_name'];
$out = file_get_contents($file1);
$file2 = "molecule.txt";
$filehandle= fopen($file2, "w+") or die("Unable to open file!");
fwrite($filehandle, $out);
fclose($filehandle);
echo "File UPLOADED SUCESSFULLLY";
?>
<form method="POST" action="molecules.html">
<br/ ><br />
<input type="submit" name="structure" value="View file" />
</form>
<?php
// Capture the file's temp path and name
$file1 = $_FILES['file']['tmp_name'];
// Reads entire file into a string
$out = file_get_contents( $file1 );
// Target file
$file2 = "molecule.txt";
// Open file for reading and writing
$filehandle = fopen( $file2, "w+" );
//Reads entire file into an array
$file_contents = file( $file1 );
$count = count( file( $file1 ) );
// Write a string to a file
file_put_contents( $file2, $file_contents );
$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";
// Binary-safe file write
fwrite($filehandle, $newline);
}
}
}
// Close open file pointer
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 />
<input type="submit" name="structure" value="View file" />
</form>
看起来像是数据库中的数据如果您试图覆盖文件内容,则以写入模式而不是追加模式打开文件。能否共享您的molecular.html
文件代码?molecular.html文件中的代码用于绘制化合物的三维结构。这是一个大文件,我如何附加这些文件…:(它将删除molecular.txt文件的内容我想覆盖molecular.txt文件的内容。是的,这将删除molecular.txt文件中的旧内容,并将您的新内容添加到文件中,据我所知,这就是覆盖的含义当然……但它不会在molecular.txt文件中写入新内容。您希望在哪里写入新内容内容呢?谢谢你的回复,我已经试过了……但它不会更新molecle.txt文件的内容。你想a)在现有内容的末尾添加新内容,同时保持原有内容不变,还是b)用新内容替换旧内容删除旧内容我用完整的代码更新了它,代码也很短。你可以试试这个。它满足选项b。