0

当我浏览页面中的文件时,我需要覆盖 .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文件的内容。

我还需要补充什么吗?请帮我。

提前致谢。

4

2 回答 2

0

代替

$filehandle = fopen($file2, "a"); // this mode means append content 

利用

$filehandle = fopen($file2, "w"); //this mode means write file by over writing the previous content
于 2015-07-13T06:20:19.340 回答
0
$filehandle = fopen($file2, "w+");

//w+ : 打开读写;将文件指针放在文件的开头并将文件截断为零长度。如果该文件不存在,请尝试创建它。

参考: http: //php.net/manual/en/function.fopen.php

注意 - 在 'w' 模式下使用 fopen 不会像您预期的那样更新文件的修改时间(filemtime)。您可能希望在写入并关闭更新其修改时间的文件后发出 touch()。这在缓存情况下可能变得至关重要。

我建议两种方法

方法 - 1(文件处理,最简单的方法;考虑到您没有从内容中进行任何逻辑操作或字符串匹配,被读出)

分子.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>

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>

方法 - 2(符合您的要求,您正在执行一些字符串搜索)

分子.html - 包含上传文件表单的页面

<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
// 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>
于 2015-07-13T06:45:45.117 回答