在php中覆盖.txt文件的内容

时间:2015-07-13 06:17:49

标签: 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);

$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文件的内容。

我还需要添加更多内容吗?请帮帮我。

先谢谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

$filehandle = fopen($file2, "w+");

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

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

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

我建议采用两种方法

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

molecules.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 (符合您的要求,您正在进行字符串搜索)

molecules.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>