dynamic-multiple-file-upload

Gesponsorde koppelingen

PHP script bestanden

  1. dynamic-multiple-file-upload

« Lees de omschrijving en reacties

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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
    ini_set ('display_errors', 1);
    error_reporting (E_ALL);
    $title = 'Dynamic Multiple File Uploads';
    $msgs = array ();
    $errs = array ();
    /*
        Constanten voor exif_imagetype ()
        1      IMAGETYPE_GIF
        2     IMAGETYPE_JPEG
        3     IMAGETYPE_PNG
    */

    $allowed = array (IMAGETYPE_JPEG);

    function
resize ($path, $dst_w) {
        list ($src_w, $src_h) = getimagesize ($path);
        if ($dst_w < $src_w) {
            $ratio = $dst_w / $src_w;
            $dst_h = ceil ($ratio * $src_h);
            $src = imagecreatefromjpeg ($path);
            $dst = imagecreatetruecolor ($dst_w, $dst_h);
            imagecopyresampled ($dst, $src, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h);
            imagejpeg ($dst, $path, 80);
            imagedestroy ($src);
            imagedestroy ($dst);
        }
    }

    
    // business logic
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $n = count ($_FILES['userfile']['error']);
        for ($i = 0; $i < $n; $i++) {
            if (!$_FILES['userfile']['error'][$i]) {
                $tmp = $_FILES['userfile']['tmp_name'][$i];
                $name = $_FILES['userfile']['name'][$i];
                $dir = 'images/';
                if (!in_array (exif_imagetype ($tmp), $allowed)) {
                    array_push ($errs, 'Sorry, alleen JPG');
                }

                elseif ($_FILES['userfile']['size'][$i] > 50000) {
                    array_push ($errs, 'Sorry, 50 KB max');
                }

                elseif (file_exists ($dir . $name)) {
                    array_push ($errs, $name . ' bestaat al!');
                }

                else {
                    $path = $_SERVER['DOCUMENT_ROOT'] . '/' . $dir . $name;
                    if (is_uploaded_file ($tmp)) {
                        if (move_uploaded_file ($tmp, $path)) {
                            chmod ($path, 0644);
                            resize ($path, 568);
                            array_push ($msgs, 'Geupload: ' . $name);
                            array_push ($msgs, '<img style="border: 1px solid #000; " src="' . $dir . $name . '">');
                        }
                    }
                }
            }
        }
    }

?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Jan Koehoorn | <?php echo $title; ?></title>
    
    <link rel="stylesheet" type="text/css" media="screen" href="reset.css" />
    <link rel="stylesheet" type="text/css" media="screen" href="oop.css" />
    <style type="text/css">
        form                    {padding: 10px;}
        ul.msgs                    {margin: 10px; padding: 10px; border: 2px solid #00f; background: #99f;}
        ul.errs                    {margin: 10px; padding: 10px; border: 2px solid #f00; background: #f99;}
        ul.msgs li,
        ul.errs li                {color: #000;}
    </style>
    
    <script type="text/javascript" src="mootools/mootools.js"></script>
    <script type="text/javascript">
        window.addEvent ('domready', function () {
            $$('a.add').each (function (item) {
                item.addEvent ('click', function (e) {
                    e = new Event (e).preventDefault ();
                    var p = item.getParent ();
                    var clone = p.clone().injectBefore(p);
                    var a = clone.getLast ();
                    a.remove ();
                    var remove = document.createElement ('a');
                    remove.innerHTML = 'verwijderen';
                    remove.href = '#';
                    remove.addEvent ('click', function (e) {
                        e = new Event (e).preventDefault ();
                        this.getParent ().remove ();
                    });
                    clone.appendChild (remove);
                    var f = p.getFirst ();
                    f.value = '';
                });
            });
        });
    </script>
</head>

<body>
    <div id="container">
        <h1>Jan Koehoorn | <?php echo $title; ?></h1>
        <p>Dit is een dynamisch multiple file upload script. Op deze manier kun je 1 of meer bestanden tegelijk uploaden.</p>
        <p>JPG only, 50 KB max</p>
        <?php
            if (!empty ($errs)) {
                echo '<ul class="errs">';
                foreach ($errs as $err) {
                    echo '<li>' . $err . '</li>';
                }

                echo '</ul>';
            }

            if (!empty ($msgs)) {
                echo '<ul class="msgs">';
                foreach ($msgs as $msg) {
                    echo '<li>' . $msg . '</li>';
                }

                echo '</ul>';
            }

        ?>

        <form id="my_form" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>" enctype="multipart/form-data">
            <p>
                <input name="userfile[]" type="file" size="60" /> <a class="add" id="toevoegen" href="#">toevoegen</a>
            </p>
            <p>
                <input type="submit" value="upload" />
            </p>
        </form>
    </div>
</body>
</html>

 
 

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.