使用 php 显示 csv 文件时获得"Warning: Invalid supplied for foreach"



我必须使用以下代码确保此文件显示在带有php的html表中。但是,当我运行代码 I 时,会收到以下警告:

警告:第 18 行的/Applications/XAMPP/xamppfiles/htdocs/test/index.php 中的 foreach () 无效

我该如何解决这个问题?

菲律宾代码:

<?php
if ( isset($_POST["submit"]) ) {
   if ( isset($_FILES["file"])) {
        if ($_FILES["file"]["error"] > 0) {
            echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
        }
        else {
                 //Print file details
             echo "Upload: " . $_FILES["file"]["name"] . "<br />";
             echo "Type: " . $_FILES["file"]["type"] . "<br />";
             echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
              $data = [];
             foreach ($_FILES["file"]["name"] as $line) {
              $data[] = str_getcsv($line); /* <-- ERROR LINE */
               }
            }       
     } else {
             echo "No file selected <br />";
     }
    }
?>
<table width="600">
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
<tr>
<td width="20%">Select file</td>
<td width="80%"><input type="file" name="file" id="file" /></td>
</tr>
<tr>
<td>Submit</td>
<td><input type="submit" name="submit" /></td>
</tr>
</form>
</table>

in Action Call display.php显示.php

<?php 
 $filename=$_GET['filename'];
 echo "<html><body><table>nn";
 $f = fopen($filename, "r");
 while (($line = fgetcsv($f)) !== false) {
   echo "<tr>";
   foreach ($line as $cell) {
   echo "<td>" . htmlspecialchars($cell) . "</td>";
  }    
   echo "</tr>n";
 }
 fclose($f);
 echo "n</table></body></html>";

相关内容

最新更新