I have an HTML file upload form that accepts a file and submits it to a database through PHP, which works fine. The code:
<form method="post" action="confirm.php" enctype="multipart/form-data">
<label>Upload File</label>
<input type="file" name="file">
<input type="submit" name="submit" value="Preview" formtarget="_blank">
</form>
My issue is that i need to combine the preview php and the submission php. Both work independently using the $file but I'm trying to make it so the user uploads -> hits the preview button -> previews the info -> hits submit -> info goes into database, but the upload php will say the file is invalid. I used some PHP code to display the contents in an html table to verify the information before submitting it to the database, like so:
<body>
<div class="phpTableWrap">
<?php
$server = "localhost";
$user = "root";
$pw = "root";
$db = "uwsTest";
$connect = mysqli_connect($server, $user, $pw, $db);
if ($connect->connect_error) {
die("Connection failed: " . $conn->connect_error);
}else{
echo'success!';
}
if(isset($_POST['submit']))
{
ini_set('auto_detect_line_endings', true);
$file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");
$maxPreviewRows = PHP_INT_MAX; // this will be ~2 billion on 32-bit system, or ~9 quintillion on 64-bit system
$hasHeaderRow = true;
echo '<table>';
if ($hasHeaderRow) {
$headerRow = fgetcsv($handle);
echo '<thead><tr>';
foreach($headerRow as $value) {
echo "<th>$value</th>";
}
echo '</tr></thead>';
}
echo '<tbody>';
$rowCount = 0;
while ($row = fgetcsv($handle)) {
echo '<tr>';
foreach($row as $value) {
echo "<td>$value</td>";
}
echo '</tr>';
if (++$rowCount > $maxPreviewRows) {
break;
}
}
echo '</tbody></table>';
}
?>
</div>
Is there a way to share the uploaded file with both PHP files so that I can insert data from the preview page?