[英]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.