forked from RonnieSan/uploadify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uploadifive-image-only.php
66 lines (49 loc) · 1.54 KB
/
uploadifive-image-only.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
<?php
/*
UploadiFive
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
*/
/*
IMPORTANT: This script requires the PHP GD library
*/
// Set the uplaod directory
$uploadDir = '/uploads/';
function errorHandler($errno, $errstr, $errfile, $errline) {
// In this script, the silently suppress any error generated by getimagesize
// which will throw an error if the "image" isn't an image
// ie doesn't have a valid width / height
/* Don't execute PHP internal error handler */
return true;
}
$old_error_handler = set_error_handler("errorHandler");
// Check if the file has a width and height
function isImage($tempFile) {
// Get the size of the image
$size = getimagesize($tempFile);
if (isset($size) && $size[0] && $size[1] && $size[0] * $size[1] > 0) {
return true;
} else {
return false;
}
}
if (!empty($_FILES)) {
$fileData = $_FILES['Filedata'];
if ($fileData) {
$tempFile = $fileData['tmp_name'];
$uploadDir = $_SERVER['DOCUMENT_ROOT'] . $uploadDir;
$targetFile = $uploadDir . $fileData['name'];
// Validate the file type
$fileTypes = array('jpg', 'jpeg', 'gif', 'png'); // Allowed file extensions
$fileParts = pathinfo($fileData['name']);
// Validate the filetype
if (in_array(strtolower($fileParts['extension']), $fileTypes) && filesize($tempFile) > 0 && isImage($tempFile)) {
// Save the file
move_uploaded_file($tempFile, $targetFile);
echo 1;
} else {
// The file type wasn't allowed
echo 'Invalid file type.';
}
}
}
?>