首页 > 解决方案 > 尝试将大型 html 文档与来自表单的输入拼凑在一起

问题描述

我正在尝试从其各个部分构建文档。目标文档未获取任何内容。

你能告诉我我在这里做错了什么吗?

我有一个begin.txt内容HTML和一个end.txt我想继续将新代码(newarticle.txt)添加到开头的内容,以便每次添加新代码时它都会向前流动。然后我想把它全部放在一起articles.html

<?php
$v1 = $_POST["url"]; //You have to get the form data
$v2 = $_POST["description"];
$v3 = $_POST["date"];

$destination = fopen('articles.html', 'w+');
$begin = fopen('begin.txt','w+');
$end = fopen('end.txt','w+');
$currentend = file_get_contents('/end.txt');
$currentbegin = file_get_contents('/begin.txt');

$file = fopen('newarticle.txt', 'w+'); //Open your .txt file
ftruncate($file, 0); //Clear the file to 0bit
// $content =  <li><a href=$v1. PHP_EOL .$v2. PHP_EOL .$v3;

$build1 = <<<EOF
<li><a href="$v1">$v2</li>
<li>$v3</li>
EOF;

fwrite($file , $build1); //Now lets write it in there
fclose($file ); //Finally close our .txt

$file = "newarticle.txt";
$currentfile = file_get_contents($file);

$build2 = <<<EOF
$build1
$currentend
EOF;

ftruncate($end, 0); //Clear the file to 0bit
fwrite ($end , $build2);
fclose ($end );

$updatedend = 'end.txt';
$updatedendcontent = file_get_contents($updatedend);
ftruncate($destination, 0);

$build3 = <<<EOF
$currentbegin
$updatedendcontent
EOF;

fwrite ($destination , $build3);
fclose ($destination );
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>

修复它,这有效

<?php
$v1 = $_POST["url"]; //You have to get the form data
$v2 = $_POST["description"];
$v3 = $_POST["date"];

// $begin = fopen('begin.txt','w+');
$currentend = file_get_contents('end.txt');
$currentbegin = file_get_contents('begin.txt');

$file = fopen('newarticle.txt', 'w+'); //Open your .txt file
ftruncate($file, 0); //Clear the file to 0bit
// $content =  <li><a href=$v1. PHP_EOL .$v2. PHP_EOL .$v3;

$build1 = <<<EOF
<li><a href="$v1">$v2</li>
<li>$v3 by Accounting Today</li>
EOF;

fwrite($file , $build1); //Now lets write it in there
fclose($file ); //Finally close our .txt

//$file = "newarticle.txt";
$currentfile = file_get_contents('newarticle.txt');

$build2 = <<<EOF
$build1
$currentend
EOF;

$end = fopen('end.txt','w+');
ftruncate($end, 0); //Clear the file to 0bit
fwrite ($end , $build2);
fclose ($end );

//$updatedend = 'end.txt';
$updatedendcontent = file_get_contents('end.txt');


$build3 = <<<EOF
$currentbegin
$updatedendcontent
EOF;

$destination = fopen('articles.html', 'w+');
ftruncate($destination, 0);
fwrite ($destination , $build3);
fclose ($destination );
die(header("Location: ".$_SERVER["HTTP_REFERER"]));
?>

标签: phphtmlfopenfwrite

解决方案


推荐阅读