image-upload-functie

Gesponsorde koppelingen

PHP script bestanden

  1. image-upload-functie

« Lees de omschrijving en reacties

De functie:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?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;    
    
}


?>

Stukje code om de functie aan te roepen:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?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.";
        
    }    
    
}


?>

De HTML Code:
Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
<form method="POST" enctype="multipart/form-data" action="upload.php">
    <input type="file" name="objUpload" />
    <input type="submit" name="objSubmit" value="Upload Plaatje" />
</form>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.