Click here to Skip to main content
14,454,989 members
Rate this:
Please Sign up or sign in to vote.
See more:
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/>";
			}
		}		
	}
?>
Posted
Updated 13-Feb-20 23:15pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

if (isset($_FILES["files"]) == false)
{
   echo "Please select file to upload!";
   return;
}
If you get to the echo line, it means that the global $_FILES["files"] is not set.

Here's a link to a page explaining how to deal with uploads in a more secure way:
PHP: Handling file uploads - Manual[^]
Re-check twice the name attribute of the input tag on HTML side. According to your PHP code, you should have something like <input name="files" ... >.
   
v2
Comments
Xavier Benedict Astor 17-Feb-20 1:32am
   
Hello, already tried editing the "echo "Please select"" part of the code. Also upon checking, seems my html form code is all good. But unfortunately, still getting the same result.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100