テーブルを含むテキストファイルをアップロードして同じページに表示するための次の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);