In my file upload script, I currently have all the files going to an /upload directory. Is it possible for me to upload it to a database as well as the upload directory so it is easier for me to manage?
<?php
require 'header.php';
?>
<center>
<?php
$uniqueid = uniqid();
$file = $uniqueid. basename( $_FILES["fileToUpload"]["name"]);
$target_dir = "uploads/$uniqueid";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
//echo "File is not an image.";
// $uploadOk = 1;
}
}
?>
<br>
<?php
// Check if file already exists
if (file_exists($target_file)) {
echo " Sorry, the file already exists or";
$uploadOk = 0;
}
?>
<br>
<?php
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "txt") {
echo "Sorry, only JPG, JPEG, PNG, GIF and TXT files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "your file was not uploaded correctly. Please try again or contact support!.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "Your file has been uploaded
<br><br><br><br>
<img src='uploads/$file'>";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
</center>