So I have searched a lot about this, but can't find any good answer. I have a hrm, where I add Users and each of them has their info like name, age, address, etc etc. I also add couple of files, like: image, ID card (jpg/pdf), Tax card (pdf), Agreement document (pdf) and there are couple of more.
Each of these documents have their seperate input field, and each will be uploaded in a different folder on the server. I got it working, but I am not sure if my method is the right way:
Basic Info (there are lot of inputs here so I won't list them all):
if(isset($_POST['btnsave'])){
$id = $_POST['w_rid'];
$name = $_POST['w_name'];
$surname = $_POST['w_surname'];
$position = $_POST['w__position'];
after these infos, I have my file uploads:
$userID = $_FILES['user_id']['name'];
$id_tmp_dir = $_FILES['user_id']['tmp_name'];
$idSize = $_FILES['user_id']['size'];
$userTaxCard = $_FILES['user_taxcard']['name'];
$taxCard_tmp_dir = $_FILES['user_taxcard']['tmp_name'];
$taxCardSize = $_FILES['user_taxcard']['size'];
$userAgreement = $_FILES['user_agreement']['name'];
$agreement_tmp_dir = $_FILES['user_agreement']['tmp_name'];
$agreementSize = $_FILES['user_agreement']['size'];
$imgFile = $_FILES['user_image']['name'];
$img_tmp_dir = $_FILES['user_image']['tmp_name'];
$imgSize = $_FILES['user_image']['size'];
Currently I have 18 different file upload inputs, with different names
Now I have the part to upload the files, and for each of those 18 file inputs, I have this block of code which I just repeat and replace the names:
if($_FILES['user_id']['error'] == 0) {
$upload_dir_id = 'user_id/';
$idExt = strtolower(pathinfo($userID,PATHINFO_EXTENSION));
$valid_extensions_id = array('pdf'); // valid extensions
$userid = $name . "-id-" . rand(1000,1000000).".".$idExt;
if(in_array($idExt, $valid_extensions_id)){
if($idSize < 5000000) {
move_uploaded_file($id_tmp_dir,$upload_dir_id.$userid);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
}
You can imagine how big the code is
And this is the last chunk of code:
// if no error occured, continue ....
if(!isset($errMSG))
{
$stmt = $DB_con->prepare('INSERT INTO Radnici2(id, name, surname, position, userTaxCard, userAgreement, imgFile, userID) VALUES(:urid, :uime, :uprezime, :uroditelj, :usprema, :uzdravlje, :uljekarski, :upradnika)');
$stmt->bindParam(':uid',$id);
$stmt->bindParam(':uname',$name);
$stmt->bindParam(':usurname',$surname);
$stmt->bindParam(':uposition',$position);;
$stmt->bindParam(':utaxc',$usertaxc);
$stmt->bindParam(':uagreement',$useragreement);
$stmt->bindParam(':upic',$userpic);
if($stmt->execute())
{
$successMSG = "Success";
header("refresh:3;index.php"); // redirects image view page after 5 seconds.
}
else
{
$errMSG = "Error....";
}
}
Is this a valid way to upload files? It doesn't feel that this what I did is correct, or that it can be greatly improved. Is there any better way? Thank you.