This is my index.php code
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script>
if ( window.history.replaceState ) {
window.history.replaceState( null, null, window.location.href );
}
</script>
</head>
<body>
<?php session_start(); ?>
<?php if(isset($_SESSION['success']) && $_SESSION['success'] == 1) { ?>
<?php unset($_SESSION['success']); ?>
<div class="jumbotron text-center">
<h1>SUCCESS</h1>
<p>CONTACT CSV UPLOADED SUCCESSFULLY</p>
<p><a onclick="resetFormDetails();" href="">Go Back to Form</a></p>
</div>
<?php } else { ?>
<div class="jumbotron text-center">
<h1>CONTACT CSV UPLOAD</h1>
<p>On Submit it may take some time to update. Be Patience!</p>
</div>
<div class="container">
<div class="row">
<div class="col-12">
<form id="contact-form" method="post" action="<?php echo 'upload.php'; ?>" enctype="multipart/form-data">
<div class="form-group">
<label for="contact-file">Upload Contact CSV</label>
<input name="contact_file" type="file" class="form-control-file" id="contact-file">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
</div>
<?php } ?>
</body>
<script>
function resetFormDetails() {
document.getElementById("contact-form").reset();
location.reload();
}
</script>
</html>
and here is my upload.php code
<?php
// SERVER SIDE
if(isset($_FILES["contact_file"]) && !empty($_FILES["contact_file"])) {
// UPLOAD FILE
$storagename = "contacts.csv";
if(!is_dir('uploads')) {
@mkdir('uploads');
} else {
if(file_exists("uploads/" . $storagename)) {
@unlink("uploads/" . $storagename);
}
}
@move_uploaded_file($_FILES["contact_file"]["tmp_name"], "uploads/" . $storagename);
unset($_FILES["contact_file"]["tmp_name"]); // UNSET TEMP
unset($_FILES["contact_file"]); // UNSET UPLOADED FILE VARIABLE
$file = fopen("contacts.csv", "r");
$csv_data = array();
while(!feof($file))
{
$csv_data[] = fgetcsv($file);
}
fclose($file);
if(file_exists("uploads/" . $storagename)) {
@unlink("uploads/" . $storagename);
}
$first_row = $csv_data[0];
$contacts = array();
if(trim($first_row[0]) == 'email' && trim($first_row[1]) == 'link') {
array_shift($csv_data);
foreach ($csv_data as $row) {
if(!empty($row)) {
$contacts[] = array(
'email' => $row[0],
'link' => $row[1]
);
}
}
} else {
echo "Fatal Error: Format Issue";
exit;
}
//hiding for the purpose
$servername = "X.X.X.X";
$username = "XYZ";
$password = "XYZ";
$dbname = "XYZ";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
foreach ($contacts as $contact) {
$sql = "INSERT INTO ch_contact_tbl(`contact_email`, `contact_url`) VALUES ('".$contact['email']."', '".$contact['link']."')";
if($conn->query($sql) === TRUE) {
continue;
} else {
echo "Error Occured";
break;
}
}
session_start();
$_SESSION['success'] = 1;
$conn->close();
header("Location: index.php");
}
I am trying to upload a csv file with some contacts details. but once the details is uploaded i don't want it would repeat without selecting file. However even i have not selected any file to upload only by just click on submit button it uploading my previous file automatically to the server.
I have tried all the ethics possible but unable to figure out the root cause of this problem.
Kindly help me to figure it out.
I have also given reset button in my form. but even i have clicked on that button. still my form is not reset and taking the value of previously file uploaded on submit. enter image description here