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>