Fileupload in PHP Useing DB
Fileupload in PHP Useing DB
php
include_once 'dbConfig.php';
$statusMsg = '';
if (isset($_POST["submit"])) {
$targetDir = "uploads/";
$fileName = basename($_FILES["fileToUpload"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if (in_array($fileType, $allowTypes)) {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$targetFilePath)) {
$insert = $db->query("INSERT INTO images (file_name, uploaded_on)
VALUES ('" . $fileName . "', NOW())");
if ($insert) {
$statusMsg = "The file " . $fileName . " has been uploaded
successfully.";
} else {
$statusMsg = "File upload failed, please try again.";
}
} else {
$statusMsg = "Sorry, there was an error uploading your file.";
}
} else {
$statusMsg = 'Sorry, only JPG, JPEG, PNG, & GIF files are allowed to
upload.';
}
}
echo $statusMsg;
?>