需要打印页面中的文件内容,并将这些结果以可下载格式存储在文本文件中我写了一个Php脚本显示文件内容表格如下
<form action="newfile.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" maxlength="25"> <br>
<input type="submit" name="upload" value="Upload">
Php脚本
$file=$_FILES['file']['tmp_name'];
$result = file_get_contents($file);
print $result;
它将打印文件内容,但我如何将其结果存储为。txt文件格式以及如何将其下载或保存在我们的机器中
请提供给我一个保存结果为。txt格式的解决方案
您需要像下面这样使用文件函数:
的例子:
<?php
$fp = fopen('data.txt', 'w');
fwrite($fp, 'hello my name is george');
fclose($fp);
?>
描述可以在这里找到:
http://php.net/manual/de/function.fwrite.php http://php.net/manual/de/function.fopen.php使用readfile()
$file=$_FILES['file']['tmp_name'];
echo readfile($file);
编辑01
<?php
$myfile = fopen("my file.txt", "w") or die("Unable to open file!");
$txt = "John Doe'n";
fwrite($myfile, $txt);
$txt = "Jane Doe'n";
fwrite($myfile, $txt);
fclose($myfile);
//move_uploaded_file(file,newlocation)
$tmp_name = $myfile["tmp_name"][$key];
move_uploaded_file($tmp_name,'./folder/')
?>
试一试:
<?php
$file=$_FILES['file']['tmp_name'];
$result = file_get_contents($file);
$write_text_file = fopen("path/to/result.txt", "w");
fwrite($write_text_file, $result);
fclose($write_text_file);
?>