directory-script

Gesponsorde koppelingen

PHP script bestanden

  1. directory-script

« Lees de omschrijving en reacties

Dir.php

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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php

/**
 * This class can perform different actions on direcoties, invoking files aswell. Please take a close look at the powerfull callback functions.
 *
 */

class Storeman_Filesystem_Dir
{
    private $_strDirname = '';
    private $_arrDirContent = array();
    private $_iDirsize = 0;
    
    private $_strTargetDir = '';
    
    /**
     * Constructor
     *
     * @param string $strDirname
     * @return Filesystem_Dir
     */

    public function __construct($strDirname)
    {

        $this->_strDirname = Storeman_Filesystem_Dir::FilterDirname($strDirname);
        
        if(!is_dir($this->_strDirname)){
            self::CreateDir($this->_strDirname);
        }
    }

    
    static public function FilterDirname( $strDirname ){
        $strDirname = trim($strDirname);
        
        if( substr($strDirname, -1, 1) != '/' ){
            $strDirname .= '/';
        }

        
        return $strDirname;
    }

    
    /**
     * Create director(y/ies), can create multiple folders in-depth
     *
     * @param string $strDirname
     */

    static public function CreateDir($strDirname)
    {

        $strCurrentDir = '';
        // Replace backslashes by normal slashes
        $strDirname = str_replace('\\', '/',$strDirname);
        
        $arrDir = explode('/', $strDirname);
        
        foreach($arrDir as $strSubdirname)
        {

            $strCurrentDir .= $strSubdirname.'/';
            
            if(!is_dir($strCurrentDir)) mkdir($strCurrentDir, 0777);
        }
        
    }

    
    /**
     * Get the content of a directory using an associative array. array('folder1'=>array('folder1_1'=>array('file1.php','file2.php'), 'file1.php')
     *
     * @param array $arrFolderContent    used
     * @return unknown
     */

    public function GetDirContent( $iLevel = -1 )
    {
    
        $arrFolderContent = array();
        
        if(($dh = opendir( $this->_strDirname ) ) !== false){
            while(($file = readdir($dh)) !== false){
                
                if($file != '.' && $file != '..'){                        
                    if(is_dir( $this->_strDirname.$file.'/')){
                        
                        if(!isset($arrFolderContent[$file]) || !is_array($arrFolderContent[$file])){
                            $arrFolderContent[$file] = array();
                        }

                        
                        $oDir = new Storeman_Filesystem_Dir( $this->_strDirname.$file.'/' );
                        if( $iLevel > 0 ){
                            $arrFolderContent[$file] = $oDir->GetDirContent( $iLevel-1 );
                        }
elseif( $iLevel === -1 ){
                            $arrFolderContent[$file] = $oDir->GetDirContent( $iLevel );
                        }
                        
                    }
elseif(is_file( $this->_strDirname.$file)) {
                        $arrFolderContent[] = $file;
                    }
                }
            }

            
            $this->_arrDirContent = $arrFolderContent;
            return $arrFolderContent;
        
        }
else{
            return false;
        }
    }

    
    /**
     * Using this to perform a function or method on a file. The callback function will get two parameters, the directoryname and the filename. The files and folders will be processed top-down, that is: index.php will be processed before main/index.php.
     *
     * @param mixed $callback
     * @param integer $iLevel, deepness of applying, -1 no limit.
     * @return true or false
     */

    public function CallbackFiles( $callback, $iLevel = -1 ){
        
        if( !is_callable($callback) ){
            trigger_error('Calling to undefined function: '.print_r($callback, true), E_USER_WARNING);
            return false;
        }

        
        
        
        if(($dh = opendir( $this->_strDirname ) ) !== false){
            while(($file = readdir($dh)) !== false){
                
                if($file != '.' && $file != '..'){                        
                    
                    if(is_file( $this->_strDirname.$file) ) {
                        call_user_func( $callback, $this->_strDirname, $file);
                    }
                    
                }
            }

            
            rewinddir($dh);
            
            while(($file = readdir($dh)) !== false){
                
                if($file != '.' && $file != '..'){                        
                    if(is_dir( $this->_strDirname.$file.'/') ){
                                                
                        $oDir = new Storeman_Filesystem_Dir( $this->_strDirname.$file.'/' );
                        
                        if( $iLevel > 0 ){
                            $oDir->CallbackFiles( $callback, $iLevel-1 );
                        }
elseif( $iLevel === -1 ){
                            $oDir->CallbackFiles( $callback, $iLevel );
                        }
                        
                    }
                }
            }

            
            closedir( $dh );
            
            return true;
        
        }
else{
            return false;
        }
    }

    
    /**
     * Using this to perform a function or method on a direcotry. The function will get one parameter, the directoryname . The deepest directory will be done first.
     *
     * @param mixed $callback
     * @param integer $iLevel, deepness of applying, -1 no limit.
     * @return true or false
     */

    public function CallbackDirectories( $callback, $iLevel = -1 ){
        if( !is_callable($callback) ){
            trigger_error('Calling to undefined function: '.print_r($callback, true), E_USER_WARNING);
            return false;
        }

        
        if(($dh = opendir( $this->_strDirname ) ) !== false){
            while(($file = readdir($dh)) !== false){
                
                if($file != '.' && $file != '..'){                        
                    
                    if(is_dir( $this->_strDirname.$file.'/')){
                                                
                        $oDir = new Storeman_Filesystem_Dir( $this->_strDirname.$file );
                        
                        if( $iLevel > 0 ){
                            $oDir->CallbackDirectories( $callback, $iLevel-1 );
                        }
elseif( $iLevel === -1 ){
                            $oDir->CallbackDirectories( $callback, $iLevel );
                        }
                        
                    }
elseif(is_file( $this->_strDirname.$file)) {
                        // Skip file processing
                    }
                }
            }

            closedir( $dh );
            
            call_user_func( $callback, $this->_strDirname );
            return true;
        }
else{
            return false;
        }
    }

    
    /**
     * Get the total size of a directory, checking all available files
     *
     * @return Total directorysize in Bytes
     */

    public function GetSize( )
    {

        $this->_iDirsize = 0;
        $this->CallbackFiles( array( $this, 'GetFilesize') );
        
        return $this->_iDirsize;
    }

    
    /**
     * Remove the current directory and all its files
     *
     */

    public function Remove()
    {

        $this->CallbackFiles( array( $this, 'RemoveFile') );
        $this->CallbackDirectories( array( $this, 'RemoveDirectory') );
    }

    
    /**
     * Remove remove all the files and directories from current directory
     *
     */

    public function Clean()
    {

        if(($dh = opendir( $this->_strDirname ) ) !== false){
            while(($file = readdir($dh)) !== false){
                
                if($file != '.' && $file != '..'){                        
                    
                    if(is_dir( $this->_strDirname.$file.'/')){
                                                
                        $oDir = new Storeman_Filesystem_Dir( $this->_strDirname.$file );
                        $oDir->CallbackFiles( array( $this, 'RemoveFile') );
                        $oDir->CallbackDirectories( array( $this, 'RemoveDirectory') );
                        
                    }
elseif(is_file( $this->_strDirname.$file)) {
                        $this->CallbackFiles( array( $this, 'RemoveFile') );
                    }
                }
            }

            closedir( $dh );
            
            return true;
        }
else{
            return false;
        }
        
    }

    
    private function RemoveFile( $strDirname, $strFilename ){
        if ( file_exists( $strDirname.$strFilename )){
            @
unlink( $strDirname.$strFilename );
        }
    }

    
    private function RemoveDirectory ( $strDirname ){
        if ( file_exists( $strDirname )){
            @
rmdir( $strDirname );
        }
    }

    
    private function GetFilesize(  $strDirname, $strFilename ){
        if( is_file( $strDirname . $strFilename ) ){
            $this->_iDirsize += filesize( $strDirname . $strFilename );
        }
    }

    
    public function Copy( $strTargetDirectory ){
        // Filter the directory and create it
        $this->_strTargetDir = self::FilterDirname( $strTargetDirectory );
        self::CreateDir( $this->_strTargetDir );
        
        $this->CallbackDirectories( array($this, 'CopyDir' ) );
        $this->CallbackFiles( array($this, 'CopyFile' ) );
    }

    
    private function CopyFile( $strDirname, $strFilename ){
        $strCopyToDir = substr_replace( $strDirname, $this->_strTargetDir, 0, strlen($this->_strDirname));
        
        if ( file_exists( $strCopyToDir.$strFilename )){
            @
unlink( $strCopyToDir.$strFilename );
        }

        
        copy( $strDirname . $strFilename, $strCopyToDir . $strFilename );
    }

    
    private function CopyDir( $strDirname ){
        $strCopyToDir = substr_replace( $strDirname, $this->_strTargetDir, 0, strlen($this->_strDirname));
        self::CreateDir( $strCopyToDir );
    }
}

[
/code]
[
code]<?php
voorbeeld.php
[code]
$strDir = 'MyFolder';
$myDir = new Storeman_Filesystem_Dir( $strDir );

// Print alle bestanden in tabel
echo '<table><tr><td>Map</td><td>Bestand</td></tr>';
$myDir->CallbackFiles( 'PrintFiles' );

echo '</table>';

//Callback functie om bestanden te printen, de callback bevat twee parameters
function PrintFiles( $strDirname, $strFilename){
echo '<tr><td>'.$strDirname.'</td><td>'.$strFilename.'</td></tr>' . PHP_EOL;
}

 
 

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.