I'm working on a HTML Form in a website where I have Input Fields like "Name", "Phone", "Message" with an "Attachment" option and saving it in local directory and Retrieving/Displaying the data in another webpage. The Text Fields are saved to MySQL db and Files are being moved to Upload folder. Things are working perfectly when an Attachment is attached. But it shows extension error when only Text Fields are submitted, because the PHP script INSERTs only when Uploaded File is Moved to Destination.
I wish to make it in a way that the Text Fields are saved into database, whether the submission is With or Without Attachment. I feel I'm lacking of something silly, would really appreciate helps coming in. Please note that I wish to do this in one single form.
Below is my form processing script.
<?php
// connect to the database
$conn = mysqli_connect('localhost', 'user', 'password', 'database');
$sql = "SELECT * FROM files";
$result = mysqli_query($conn, $sql);
$files = mysqli_fetch_all($result, MYSQLI_ASSOC);
// Uploads files
if (isset($_POST['save'])) { // if save button on the form is clicked
// variables for input data
$submittedby = $_POST['submittedby'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$content = $_POST['content'];
// name of the uploaded file
$filename = $_FILES['myfile']['name'];
// destination of the file on the server
$destination = 'uploads/' . $filename;
// get the file extension
$extension = pathinfo($filename, PATHINFO_EXTENSION);
// the physical file on a temporary uploads directory on the server
$file = $_FILES['myfile']['tmp_name'];
$size = $_FILES['myfile']['size'];
if (!in_array($extension, ['zip', 'pdf', 'docx'])) {
echo "Your file extension must be .zip, .pdf or .docx";
} elseif ($_FILES['myfile']['size'] > 1000000) { // file shouldn't be larger than 1Megabyte
echo "File too large!";
} else {
// move the uploaded (temporary) file to the specified destination
if (move_uploaded_file($file, $destination)) {
$sql = "INSERT INTO files (name, size, submittedby, email, phone, content, downloads) VALUES ('$filename', $size, '$submittedby', '$email', '$phone', '$content', 0)";
if (mysqli_query($conn, $sql)) {
echo "File uploaded successfully";
}
} else {
echo "Failed to upload file.";
}
}
}
// Downloads files
if (isset($_GET['file_id'])) {
$id = $_GET['file_id'];
// fetch file to download from database
$sql = "SELECT * FROM files WHERE id=$id";
$result = mysqli_query($conn, $sql);
$file = mysqli_fetch_assoc($result);
$filepath = 'uploads/' . $file['name'];
if (file_exists($filepath)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($filepath));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize('uploads/' . $file['name']));
//This part of code prevents files from being corrupted after download
ob_clean();
flush();
readfile('uploads/' . $file['name']);
// Now update downloads count
$newCount = $file['downloads'] + 1;
$updateQuery = "UPDATE files SET downloads=$newCount WHERE id=$id";
mysqli_query($conn, $updateQuery);
exit;
}
}