-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload_img.php
More file actions
42 lines (36 loc) · 1.16 KB
/
upload_img.php
File metadata and controls
42 lines (36 loc) · 1.16 KB
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
/**
* Upload images from _POST request
*
*param Type [post name image] [Required]
*param Type [ID picture] [optional]
*param Type [path to save the image] [optional]
*param Type [extension under which the stored image] [optional]
*return True [if no errors]
*/
/**
Example of use:
$UPL_INFO = upload_img('img_name' , $id , 'upl_images', 'jpg');
if(!$UPL_INFO)exit($UPL_INFO);
*/
function upload_img($IMG, $ID = false, $PATH = 'images', $EXT = 'png'){
if(!isset($_FILES["{$IMG}"])){return false;}
if($ID){
$_PATH = $_SERVER['DOCUMENT_ROOT'] . "/{$PATH}/{$ID}{$EXT}";
}else{
$_PATH = $_SERVER['DOCUMENT_ROOT'] . "/{$PATH}/{$IMG}{$EXT}";
}
if(!is_writable($_PATH)){return "The rights to the folder you've forgotten: 3";}
$allowedExts = array("jpg", "jpeg", "gif", "png", "JPG", "JPEG", "GIF", "PNG");
if (in_array(end(explode(".", $_FILES["{$IMG}"]["name"])), $allowedExts)) {
if(is_uploaded_file($_FILES["{$IMG}"]["tmp_name"])){
if($ID){
move_uploaded_file($_FILES["{$IMG}"]["tmp_name"], $_PATH);
}else{
move_uploaded_file($_FILES["{$IMG}"]["tmp_name"], $_PATH);
}
return true;
} else {
return "Error loading file <b>{$IMG}</b>";
}
}
}