upload_class.php

Gesponsorde koppelingen

PHP script bestanden

  1. upload_class.php
  2. use.php

« 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php

/**
 * @package uploader
 *
 * @author Niels Kieviet <[email protected]>
 * @version $Revision v1.00$
 * @copyright Copyright (c) 2010, Niels Kieviet
 */


class uploader
{
    /**
     * @var Array
     */

    protected $extensions;
    /**
     * @var Array
     */

    protected $types;
    /**
     * @var Integer
     */

    protected $size;
    /**
     * @var Array
     */

    protected $errors;
    /**
     * @var String
     */

    protected $hash;
    
    /**
     * Contructor
     *
     * @param Array $extensions
     * @param Array $types
     * @param Integer $size
     */

    public function __construct( Array $extensions = array(), Array $types = array(), $size = 5000000 )
    {

        $this->errors = array();
        $this->extensions = $extensions;
        $this->types = $types;
        $this->size = $size;
    }


    /**
     * Upload the file
     *
     * @param String $file
     * @param String $path
     */

    public function upload( $file, $path )
    {

        if( $this->checkFile( $file ) ) {
            if( $this->isAllowed( $file ) && $this->pathExists( $path ) ) {
                // Extension of file
                $ext = strtolower( strrchr( $file['name'], '.' ) );
                // Make a final hash
                $hash = new hash();
                $hash->createHash();
                $this->hash = $hash->getHash();
                // Upload the file to the final folder
                if( !move_uploaded_file( $file['tmp_name'], $path.$this->hash.$ext ) ) {
                    $this->errors[] = 'Verplaats fout';
                }
            }
        }

        //var_dump ( $this->checkSize( $file ) );
    }
    
    /**
     * Check if file is allow
     *
     * @param String $file
     * @return Boolean
     */

    protected function isAllowed( $file )
    {

        $allowed = true;
        foreach( array( 'mime', 'extension', 'size' ) as $check ) {
            $method = sprintf( 'check%s', ucfirst( $check ) );
            $allowed = ( $this->$method( $file ) ) && $allowed;
        }

        return $allowed;
    }

    
    /**
     * CheckFile
     *
     * @param String $file
     * @return Boolean
     */

    public function checkFile( $file )
    {

        if( empty( $file['tmp_name'] ) ) {
            $this->errors[] = 'Select bestand';
            return false;
        }

        return true;
    }

    
    /**
     * Check MimeType
     *
     * @param String $file
     * @return Boolean
     */

    protected function checkMime( $file )
    {

        if( count( $this->types ) > 0 ) {
            if( !in_array( $file['type'], $this->types ) ) {
                $this->errors[] = 'MIME type fout';
                return false;
            }

            return true;
        }

        return true;
    }

    
    /**
     * Check extension
     *
     * @param String $file
     * @return Boolean
     */

    protected function checkExtension( $file )
    {

        if( count ( $this->extensions ) > 0 ) {
            // Check extension
            $ext = strtolower( strrchr( $file['name'], '.' ) );
                // Control if extension is allowed
                if( !in_array( $ext, $this->extensions ) ) {
                    $this->errors[] = 'Extensie fout';
                    return false;
                }

            return true;
        }

        return true;
    }

    
    /**
     * Check size
     *
     * @param String $file
     * @return Boolean
     */

    protected function checkSize( $file )
    {

        if( ctype_digit( $this->size ) ) {
            // Controle file size
            if( $file['size'] > $this->size ) {
                $this->errors[] = 'Grote bestand';
                return false;
            }
        }

        return true;
    }

    
    /**
     * Check path
     *
     * @param String $path
     * @return Boolean
     */

    protected function pathExists( $path )
    {

        if( !empty( $path ) ) {
            if( !is_dir( $path ) ) {
                $this->errors[] = 'Upload map';
                return false;
            }
        }

        return true;
    }

    
    /**
     * Give the hash
     *
     * @return String
     */

    public function hash( )
    {

        return $this->hash;
    }

    
    /**
     * Give all errors
     *
     * @return Array
     */

    public function errors( $css )
    {

        if( sizeof( $this->errors ) > 0 ) {
            $html = '<ul class="' .$css. '">';
                // Foreach array to string
                foreach( $this->errors as $error ) {
                    $html .= '<li> ' .$error. ' </li>';
                }

            $html .= '</ul>';
            return $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.