Hello, I am getting a problem on uploading a file to database and to the "upload" directory,
I have a database named "saide_db", table named "userfiles", and "upload" directory for my uploaded files. I can tell that upload.php file is working fine on local even connecting with remote database, but when I try to upload a file, it always executes only the "Please select file to upload!" function resulting to "upload" directory always empty, even though I have a file included in the upload form.
Pls check the "upload.php" code below and let me know if you find any error/s and have a suggested solution/s. Thank you.
What I have tried:
<?php
if(isset($_POST['btnSubmit'])){
$errors = array();
$extension = array("jpeg","jpg","png","gif");
$bytes = 1024;
$allowedKB = 100;
$totalBytes = $allowedKB * $bytes;
if(isset($_FILES["files"])==false)
{
echo "Please select file to upload!";
return;
}
$conn = mysqli_connect("localhost","root","","saide_db");
foreach($_FILES["files"]["tmp_name"] as $key=>$tmp_name)
{
$uploadThisFile = true;
$file_name=$_FILES["files"]["name"][$key];
$file_tmp=$_FILES["files"]["tmp_name"][$key];
$ext=pathinfo($file_name,PATHINFO_EXTENSION);
if(!in_array(strtolower($ext),$extension))
{
array_push($errors, "File type is invalid. Name:- ".$file_name);
$uploadThisFile = false;
}
if($_FILES["files"]["size"][$key] > $totalBytes){
array_push($errors, "File size must be less than 100KB. Name:- ".$file_name);
$uploadThisFile = false;
}
if(file_exists("Upload/".$_FILES["files"]["name"][$key]))
{
array_push($errors, "File already exists. Name:- ". $file_name);
$uploadThisFile = false;
}
if($uploadThisFile){
$filename=basename($file_name,$ext);
$newFileName=$filename.$ext;
move_uploaded_file($_FILES["files"]["tmp_name"][$key],"Upload/".$newFileName);
$query = "INSERT INTO userfiles(FilePath, FileName) VALUES('Upload','".$newFileName."')";
mysqli_query($conn, $query);
}
}
mysqli_close($conn);
$count = count($errors);
if($count != 0){
foreach($errors as $error){
echo $error."<br/>";
}
}
}
?>