image-upload-functie
De functie:
Code (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
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
<?php
define("MAX_SIZE", 1);
define("MIN_SIZE", 2);
define("EXACT_SIZE", 3);
function UploadImage($file, $to_url, $allowed_types = NULL, $allowed_ext = NULL, $sizes = NULL)
{
if(is_uploaded_file($file["tmp_name"]))
{
list($x, $y, $image_type) = getimagesize($file["tmp_name"]);
list($gx, $gy, $ctype) = $sizes;
$split_name = explode(".", $file["name"]);
$file_name = $file["name"];
if((($sizes == NULL) || (($ctype == MAX_SIZE) && (($x <= $gx) && ($y <= $gy))) || (($ctype == MIN_SIZE) && (($x >= $gx) && ($y >= $gy))) || (($ctype == EXACT_SIZE) && (($x == $gx) && ($y == $gy)))) && (($allowed_types == NULL) || (array_search($image_type, $allowed_types, true) !== false)) && (($allowed_ext == NULL) || (array_search(strtolower($split_name[count($split_name) - 1]), $allowed_ext) !== false)))
{
$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
while(file_exists($to_url . $file_name))
{
$split_name[0] = $split_name[0] . $pattern{ rand(0, 35) };
$file_name = implode(".", $split_name);
}
move_uploaded_file($file["tmp_name"], ($to_url . $file_name));
return $file_name;
}
}
return false;
}
?>
define("MAX_SIZE", 1);
define("MIN_SIZE", 2);
define("EXACT_SIZE", 3);
function UploadImage($file, $to_url, $allowed_types = NULL, $allowed_ext = NULL, $sizes = NULL)
{
if(is_uploaded_file($file["tmp_name"]))
{
list($x, $y, $image_type) = getimagesize($file["tmp_name"]);
list($gx, $gy, $ctype) = $sizes;
$split_name = explode(".", $file["name"]);
$file_name = $file["name"];
if((($sizes == NULL) || (($ctype == MAX_SIZE) && (($x <= $gx) && ($y <= $gy))) || (($ctype == MIN_SIZE) && (($x >= $gx) && ($y >= $gy))) || (($ctype == EXACT_SIZE) && (($x == $gx) && ($y == $gy)))) && (($allowed_types == NULL) || (array_search($image_type, $allowed_types, true) !== false)) && (($allowed_ext == NULL) || (array_search(strtolower($split_name[count($split_name) - 1]), $allowed_ext) !== false)))
{
$pattern = "1234567890abcdefghijklmnopqrstuvwxyz";
while(file_exists($to_url . $file_name))
{
$split_name[0] = $split_name[0] . $pattern{ rand(0, 35) };
$file_name = implode(".", $split_name);
}
move_uploaded_file($file["tmp_name"], ($to_url . $file_name));
return $file_name;
}
}
return false;
}
?>
Stukje code om de functie aan te roepen:
Code (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
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
<?php
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$sizes = array(60, 40, MIN_SIZE); // array(x, y, MIN_SIZE|MAX_SIZE|EXACT_SIZE)
$allowed_types = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG); // array(types van: http://nl3.php.net/manual/nl/function.exif-imagetype.php)
$allowed_ext = array("jpg", "png", "gif"); // array(ext1, ext2, ext3)
$path = "images/"; // Str: waar die geupload moet worden.
$global = $_FILES["objUpload"]; // De global.
if($filename = UploadImage($global, $path, $allowed_types, $allowed_ext, $sizes))
{
echo "Het plaatje is succesvol geupload:<br />\n<img src=\"" . $path . $filename . "\" alt=\"" . $path . $filename . "\" />";
}
else
{
echo "Het plaatje kon niet geupload worden.";
}
}
?>
if($_SERVER["REQUEST_METHOD"] == "POST")
{
$sizes = array(60, 40, MIN_SIZE); // array(x, y, MIN_SIZE|MAX_SIZE|EXACT_SIZE)
$allowed_types = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG); // array(types van: http://nl3.php.net/manual/nl/function.exif-imagetype.php)
$allowed_ext = array("jpg", "png", "gif"); // array(ext1, ext2, ext3)
$path = "images/"; // Str: waar die geupload moet worden.
$global = $_FILES["objUpload"]; // De global.
if($filename = UploadImage($global, $path, $allowed_types, $allowed_ext, $sizes))
{
echo "Het plaatje is succesvol geupload:<br />\n<img src=\"" . $path . $filename . "\" alt=\"" . $path . $filename . "\" />";
}
else
{
echo "Het plaatje kon niet geupload worden.";
}
}
?>
De HTML Code: