-1

I have to make sure that this file is displayed in a html table with php using the code below. When, however, I run the code I, get the following warning:

Warning: Invalid supplied for foreach () in /Applications/XAMPP/xamppfiles/htdocs/test/index.php on line 18

How do I solve this?

Php Code:

<?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>
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
riki
  • 1,502
  • 5
  • 17
  • 45

1 Answers1

0

in Action call display.php display.php

<?php 
 $filename=$_GET['filename'];
 echo "<html><body><table>\n\n";
 $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>";
shreyash
  • 1
  • 1
  • 5