DEV Community

Discussion on: HOW CAN I LIMIT THE MAXIMUM FILE UPLOAD LIMIT PER USER IN PHP.

Collapse
 
vinhpedro profile image
Elijah Emmanuel

Thanks I really appreciate
But is it possible to use SQL to count the number of times the user have uploaded previously, then check it from the location and pass the error?

Collapse
 
blackscorp profile image
Vitalij Mik

yes it is but, you allow currently SQL injections in your code. i can create a file with SQL Code in the name and can inject it into your database.

And also the ID.

First your SQL Command should look like this

$sql = "INSERT INTO business(image,id) VALUES(?,?) ON DUPLICATE KEY UPDATE image = VALUES(image)";  
//Here we send the SQL to the database and say that in ? those are variables
$statement = mysqli_prepare($conn,$sql); 
//Here we send the variables in extra function so the database can "secure" them. we also say what kind of variables this is, s = string, i= integer, so filename should be a text and id should be a number
mysqli_stmt_bind_param($conn,"si",$filename,$id);
mysqli_stmt_execute($statement); //now we can execute the SQL in the database with secure variables
Enter fullscreen mode Exit fullscreen mode

this way you will produce multiple entries in your Database. now you can call following SQL before your script starts

$sql ="SELECT COUNT(filename) as countFiles FROM business WHERE id = ?
LIMIT 1
";
$statement = mysqli_prepare($conn,$sql);
mysqli_stmt_bind_param($conn,'i',$id);
mysqli_stmt_execute($statement);
$result = mysqli_stmt_get_result($statement);
$dataFromDatabase = mysqli_fetch_assoc($result);
$limitReached = $dataFromDatabase['countFiles'] > 4;
if($limitReached) {
//TODO
}
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
vinhpedro profile image
Elijah Emmanuel

II really thank you vary much, u are the best