-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadProcessing.php
79 lines (70 loc) · 2.48 KB
/
uploadProcessing.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
function uploadFile($target_dir, $filename) {
$target_file = $target_dir . basename($_FILES[$filename]["name"]);
$uploadOk = 1;
$path_parts = pathinfo($target_file); // new line added by JDS
//$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); original code.
$imageFileType = $path_parts['extension'];
// line above was suggested in PHP manual pages online. pathinfo returns an array!
// Check if image file is a actual image or fake image
$tmp_file = $_FILES[$filename]["tmp_name"];
$check = getimagesize($_FILES[$filename]["tmp_name"]);
//print "Checking image size of $tmp_file. Size is $check[0]<br>";
if($check !== false) {
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if (file_exists($target_file)) {
echo "File already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES[$filename]["size"] > 10000000) { // I changed this to 10 MB to accomodate hi res images.
echo "Your file is too large."; // I also had to change the setting in PHP.ini for max upload size
$uploadOk = 0;
}
// Allow certain file formats // JDS: I had to change these to uppercase.
$imageFileType = strtoupper($imageFileType);
if($imageFileType != "JPG" && $imageFileType != "PNG" && $imageFileType != "JPEG"
&& $imageFileType != "GIF" ) {
echo "Image File type is $imageFileType. Only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Additional Error Checking Added by Joan:
if ($_FILES[$filename]['error'] > 0) {
$uploadOk = 0;
switch ($_FILES[$filename]['error']) {
case 1:
echo "File exceeded upload_max_filesize"; break;
case 2:
echo "File exceeded max_file_size."; break;
case 3:
echo "File ony partially uploaded."; break;
case 4:
echo "No file uploaded."; break;
case 6:
echo "No temp directory specified."; break;
case 7:
echo "Upload failed. Cannot write to disk"; break;
case 8:
echo "A PHP extension blocked the file upload."; break;
}
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES[$filename]["tmp_name"], $target_file)) {
$newfilename = basename($_FILES[$filename]["name"]);
$imgLocation = $target_dir . $newfilename;
} else {
echo "There was an error uploading your file.";
}
}
return $uploadOk;
}
?>