【发布时间】:2016-09-22 04:57:46
【问题描述】:
例如:用户在文本区输入如下:
123111_Testingzone
123222_Testinghouse
123333_Testingcity
输出应在下载的文件.txt 中,如下所示;
My testname is 123111
My testname is 123222
My testname is 123333
我的过程:
我尝试在 html 形式的一个文本区域中的每一行中进行多个输入。试图在 PHP 中分解它并循环它以将其与预定义的文本一起使用并作为文件输出。
-
HTML 代码:
<!DOCTYPE html> <html> <head> <title>Test/title> </head> <header> <h1>Test_Page</h1> </header> <body> <form action="abc.php" method="post"> <h3>Option1</h3> Test Name: <textarea name="testname1"></textarea> <input type="submit" value="Download"> </form> </body> </html>对于 PHP 代码,我只想要文本区域中每行的前 6 个字母,并在一个文件中输入和输出多个文本时重复它。
-
PHP 代码:
<?php $testname = $_POST["testname1"]; $array = explode('\n',$testname); $filename = "downloaded_file.txt"; $testid = substr("{$array}",0,6); foreach ($testid as $content) { $contenter = "My test name is {$content}"; } $f = fopen($filename, 'w'); fwrite($f, $contenter); fclose($f); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Length: ". filesize("$filename").";"); header("Content-Disposition: attachment; filename=$filename"); header("Content-Type: application/octet-stream; "); header("Content-Transfer-Encoding: binary"); readfile($filename); ?>
我不知道我的代码错在哪里以及如何更正?请帮忙。
【问题讨论】:
-
循环
$array检查前六个字符的所有行