-
Notifications
You must be signed in to change notification settings - Fork 0
/
validateImg.php
53 lines (38 loc) · 1.19 KB
/
validateImg.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
// Either returns upload path, or false
function validateImage($photo){
$target_file = basename($photo["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
//Upload code borrowed from https://www.w3schools.com/php/php_file_upload.asp
// Check if image file is a actual image or fake image
$check = getimagesize($photo["fileToUpload"]["tmp_name"]);
if($check !== false) {
// echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
return false;
}
// Check file size
if ($photo["fileToUpload"]["size"] > 16777215) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
return false;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
$uploadOk = 0;
echo "File must be an Image (png, jpg, jpeg)";
return false;
}
// Check if $uploadOk is set to 0 by an error
if (!$uploadOk == 0) {
return $imageFileType;
}
}
?>