[英]how to read/extract the values of a text file which is uploaded on to a html form into another text file using php?
我有以下HTML,JavaScript和PHP代码来上载具有表格的文本文件,并将其显示在同一页面中,
HTML:
<panel action="#" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" onchange="return ajaxFileUpload(this);">
</panel>
<iframe width="300px" height="300px" name="upload_iframe" id="upload_iframe" ></iframe>
JavaScript:
function ajaxFileUpload(upload_field)
{
var filename = upload_field.value;
upload_field.form.action = 'upload_file.php';
upload_field.form.target = 'upload_iframe';
upload_field.form.submit();
return true;
}
PHP:upload_file.php
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$filepath = $_FILES["file"]["name"];
if (file_exists($filepath)) {
$file = fopen($filepath, 'r');
echo '<table border=1>';
while (!feof($file)) {
$line = fgets($file);
$first_char = $line[0];
if ($first_char != '*' && $first_char != '^' && trim($line) != '') {
if (strstr($line, '|')) {
$split = explode('|', $line);
echo '<tr>';
foreach($split as $line) {
echo '<td>'.$line.'</td>';
}
echo '</tr>';
} else {
echo '<tr><td>'.$line.'</td></tr>';
}
}
}
echo '</table>';
} else {
echo 'the file does not exist';
}
}
?>
现在,我有以下php代码可以读取/提取文件内容并将其显示在另一个文本文件中,但是我无法使用此代码获取输出。 当我尝试从html页面的输入中读取/提取值时,此代码可以正常工作,我是否在朝着正确的方向思考? 我怎样才能做到这一点?
PHP:
file_put_contents($file, "\n File Contents: ", FILE_APPEND | LOCK_EX);
//$ret = file_put_contents($file, $_POST['file'], FILE_APPEND | LOCK_EX);
ajaxFileUpload无法发送$ _FILES变量(仅适用于exporer或旧的浏览器)
我建议您使用正常的文件输入来上传文件。 发送表格后,您无需将上传的文件复制到目录中。
仅使用$ _FILES [“ file”] [“ tmp_name”]的file_get_contents获取临时文件内容;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.