I have 2 questions.
my first question-- I am using a HTML form to upload image files. Now i am using validation to allow the file types. When i was searched through google i found this.
But when I change with "all file" When selecting a picture, Users can send data that is not an image. My question is here, How to make validation for upload only image and big file below 2mb
my second question-- How to validate when people don't fill the file, I've tried this "!isset($picture))" But it doesn't work.
[code]-->start
<!DOCTYPE html>
<html>
<head>
<?php
require_once "db.php";
require_once "function.php";
if( isset($_POST['submit']) ){
$name = $_POST['name'];
$info = $_POST['info'];
$picture = $_FILES['picture'];
if(!empty(trim($name)) && !empty(trim($info)) ) {
if(add_data($name, $info, $picture)) {
echo "success";
}else{
echo "fail";
}
}else{
echo 'The data is empty';
}
}
?>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>Name</td>
<td>:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>Info</td>
<td>:</td>
<td><textarea name="info"></textarea></td>
</tr>
<tr>
<td>picture</td>
<td>:</td>
<td><input type="file" accept="image/*" name="picture"></td>
</tr>
<tr>
<td></td>
<td></td>
<td><button type="submit" name="submit">Save</button></td>
</tr>
</table>
</form>
</body>
</html>
[/code]-->end
[code]function-->start
<?php
function add_data($name, $info, $picture){
global $connect;
$name = mysqli_real_escape_string($connect, $name);
$info = mysqli_real_escape_string($connect, $info);
$filePath = "file/".basename($picture["name"]);
move_uploaded_file($picture["tmp_name"], $filePath);
$query = "INSERT INTO try (name, info, picture) VALUES ('$name', '$info', '$filePath')";
if( mysqli_query($connect, $query) ){
return true;
}else{
return false;
}
}
?>
[/code]-->end