For example: Input from user in text area will be as below:
123111_Testingzone
123222_Testinghouse
123333_Testingcity
Output should be in downloaded_file.txt and it should look like as below;
My testname is 123111
My testname is 123222
My testname is 123333
My process:
I try to take multiple input in each line in one textarea in html form. Trying to explode it in PHP and loop it to use it with predefined text and output as file.
-
HTML CODE:
<!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>
For PHP code, I want only first 6 letters of each line in text area and repeat it as many text enter and output in one file.
-
PHP CODE:
<?php $testname = $_POST["testname1"]; $array = explode(' ',$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); ?>
I dont know where my code is wrong and how to correct it? Please help.