Ftp System in OOP

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Andreas Warnaar

Andreas Warnaar

21/04/2008 21:17:00
Quote Anchor link
Ik ben een tijdje bezig om mijn Bestands beheer systeem te verbeteren http://www.phphulp.nl/php/scripts/7/987/.

en ik ben tot het idee gekomen om een ftp systeem te gaan maken met de zelfde looks en feels als mijn Bestands beheer systeem..

Alleen schiet het af en toe door mijn hoofd of ik wel de juiste kant op ga met het gebruik van OOP..

De code voor de lief hebbers. Op dit moment is het alleen maar een lijst van mappen en bestanden alle delete en hernoem functie heb ik er nog niet in verwerkt...

graag jullie ideën en opmerkingen..

Voorbeeld : http://www.andreaswarnaar.nl/map/cls_ftp.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
<?php
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
error_reporting (E_ALL);

class cls_Ftp{

    private $m_ftpUserName;
    private $m_ftpUserPass;
    private $m_ftpServer;
    public    $m_ftpConn;

    function
__construct(){
        $this->m_ftpUserName = '';//$a_Auth['UserName'];
        $this->m_ftpUserPass = ''; //$a_Auth['UserPass'];
        $this->m_ftpServer = '';//$a_Auth['Server'];
        $this->m_ftpConn = ftp_connect($this->m_ftpServer);
        $this->Connect();
    }
//End function __construct()

    function __destructor(){
        $this->Disconnect();
    }
// End function __distruct()
    
    function Connect(){
        if (!$this->m_ftpConn) {
            echo 'Fout bij het maken van verbinding';
            # Fout geen verbinding kunnen maken met $this->m_ftpServer.
        }
        if (ftp_login($this->m_ftpConn, $this->m_ftpUserName, $this->m_ftpUserPass)) {
           //echo "verbonden als ".$this->m_ftpUserName." @ ".$this->m_ftpServer;
        }else{
            echo "Kon geen verbinding maken als". $this->m_ftpUserName;
            $this->Disconnect($this->m_ftpConn);
        }
    }
//End function Connect()

    function GetStreamStatus (){
        if (isset ($this->m_ftpConn)){
            return true;
        }

    return false;
    }
// End function GetStreamStatus()

    function ChangeChmod($p_Path,$p_Chmod){
        return ftp_chmod($this->m_ftpConn, $p_Chmod, $p_Path) ? "CHMOD successful!" : false;
    }
// End function ChangeChmod()

    function DeleteFile($p_File){
        ftp_delete($this->m_ftpConn,$p_File);

    }
//End function DeleteFile()
/*
    function RenameDirFile($p_OldName, $p_NewName){
        if (ftp_rename($this->ftp_Conn, $p_OldName, $p_NewName)){
            return TRUE;
        }else{
            return FALSE;
        }
    } // End function RenameDirFile()
*/

    function DeleteDir(){} //End function DeleteDir()
/*
    function MoveFileDir($p_OldName, $p_NewName){
        if (ftp_rename($this->ftp_Conn, $p_OldName, $p_NewName)){
            return TRUE;
        }else{
            return FALSE;
        }
    } //End function MoverFileDir()
*/

    function aList($p_Path){
    
        return (isset($p_Path)) ? ftp_nlist($this->m_ftpConn,$p_Path) : "NO path given!!";

    }
//End function nList

    function aListDirFiles ($directory, $raw = false){
        if ($this->GetStreamStatus ($this->m_ftpConn) == true){
            $func    = ($raw == false) ? 'ftp_nlist' : 'ftp_rawlist';
            $chk    = $func ($this->m_ftpConn, $directory);
            if (is_array ($chk)){
                sort($chk);
                $arr= array();
                $count='';
                foreach ($chk as $file) {
                    if ($this->ftp_is_dir($file)) {
                        $count++;
                        $arr['dir'][$count]=($raw == false) ? str_replace ($directory . '/', '', $file) : $file;
                    }
                }

                $count='';
                foreach ($chk as $file) {
                    if (!$this->ftp_is_dir($file)) {
                        $count++;
                        $arr['file'][$count]['name']=($raw == false) ? str_replace ($directory . '/', '', $file) : $file;
                        $arr['file'][$count]['size']=$this->GetFileSize($file);
                        //$arr['file'][$count]['chmod']=ftp_chmod($this->m_ftpConn,'',$file);
                        $arr['file'][$count]['lastmodi']=date("m d Y H:i:s.",ftp_mdtm($this->m_ftpConn,$file));
                    }
                }

                return $arr;
            }

            return false;
        }

        return false;
    }
// End function ListDirFiles()

    function ftp_is_dir($dir) {  
        if (@ftp_chdir($this->m_ftpConn, $dir)) {
            @
ftp_chdir($this->m_ftpConn, '..');
            return true;
        }
else{
            return false;
        }
    }
// End function ftp_is_dir()

    function GetFileSize($p_File,$int=false){
        $size = ftp_size($this->m_ftpConn,$p_File);
        if($size == -1){
            $size='sss';
        }
else{
            if(!($int)){
                if ($size < 1024){
                    return round($size,2).' bytes';
                }

                elseif ($size < (1024*1024)){
                    return round(($size/1024),2).' KB';
                }

                elseif ($size < (1024*1024*1024)){
                    return round((($size/1024)/1024),2).' MB';
                }

                elseif ($size < (1024*1024*1024*1024)){
                    return round(((($size/1024)/1024)/1024),2).' GB';
                }

                elseif ($size < (1024*1024*1024*1024*1024)){
                    return round((((($size/1024)/1024)/1024)/1024),2).' TB';
                }
            }
        }

        return $size;
    }
// End function GetFileSize
    
    function ChangeDir($p_Path){
        return (ftp_chdir($this->m_ftpConn, $p_Path)) ? $return = 'het werkt': $return= 'Het werkt niet';
    }
// End function ChangeDir()

    function Disconnect(){
        ftp_close($this->m_ftpConn);
        echo 'KILL';

    }
// End function Disconnect()
}



// --------------------------------------------------------------------------------------------------
// Class name:  DirectoryListing
// --------------------------------------------------------------------------------------------------


class DirectoryListing extends cls_ftp{

    private $m_PageTitle    ='Bestands beheer';
    private $m_PageLanguage    ='NL';
    private $m_HttpIconPath    ='/images/iconen/';
    private $m_HttpRootPath    ='/';
    private $m_FtpUserName    ='';
    private $m_FtpUserPass    ='-';
    private $m_FtpHostAdres ='.nl';
    private $m_FtpDocRoot    =''; //Leeg laten voor Root
    public    $m_HtmlOutput    ='';


    function
BreadCrums($p_DirPath){
        $dir=explode('/',str_replace($this->m_FtpDocRoot,'',$p_DirPath));
        $count = count($dir) -1;
        $dir_ = '';
    // Root directory
       $HtmlOutput = '<div class="FileNavi"><a href="'.$_SERVER['PHP_SELF'].'"> Start</a>  ';
       foreach($dir AS $key =>$value){
            if (!($p_DirPath == $value )){                
                if (!empty($value)){
                    $dir_ .= '/'.$value;
                    if ($count != $key){
    // Subdirectory's
                    $HtmlOutput.= '<a href="'.$_SERVER['PHP_SELF'].'?SubDir='.$dir_.'/"> / '.$value.'</a>  ';
                    }
else{
    // Anders een schuinstreepje voor de moeite.
                    $HtmlOutput.= '/'.$value;
                    }
                }
            }
        }

        $HtmlOutput .= '</div>';
        return  $HtmlOutput;
    }

    function
HtmlDirectoryListing($p_Path){
        $header='
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml">
            <head>
             <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
             <title>'
.$this->m_PageTitle.'</title>
            <!-- CSS -->
            <link rel="stylesheet" href="css/screen.css" media="screen,projection" type="text/css" />
             <link rel="stylesheet" type="text/css" href="css/style.css" />
            <!-- JavaScript -->
            <!-- <script type="text/javascript" src="scripts/prototype.js"></script> -->
            <!-- <script type="text/javascript" src="scripts/lightbox.js"></script> -->
            <!-- <script type="text/javascript" src="scripts/popup.js" ></script> -->
            <!-- <script type="text/javascript" src="scripts/iframeAjax.js" ></script> -->
            <!-- <script type="text/javascript" src="scripts/sorttable.js"></script>-->
            <!-- <script type="text/javascript" src="/codepress/codepress.js"></script> -->
            </head>
            <body>'
;
            $this->m_HtmlOutput .= $this->BreadCrums($this->m_FtpDocRoot.$p_Path);
        $this->m_HtmlOutput .='<table class="sortable FileList" width="100%" border="0" cellspacing="1" cellpadding="0" >        
        <tr class="FileList FileListTop ">
          <td class="sorttable_nosort" style="border-bottom:solid 1px #666666; width:16px;"></td>
          <td style="border-bottom:solid 1px #666666; width:400px;">Naam</td>
          <td class="sorttable_alpha" style="border-bottom:solid 1px #666666; width:100px;">Type</td>
          <td class="sorttable_numeric" style="border-bottom:solid 1px #666666; width:50px;">Grote</td>
          <td class="sorttable_ddmm" style="border-bottom:solid 1px #666666; width:160px;">Gewijzigd op</td>
          <td class="sorttable_nosort"style="border-bottom:solid 1px #666666;">Opties</td>
         </tr>
         </thead>
         <tbody>
         <tr>
         <td colspan="4"></td>
        <td align="right"></td>'
."\n";
        
        $aList= $this->aListDirFiles($this->m_FtpDocRoot.$p_Path);
        if(isset($aList['dir'])){
            foreach($aList['dir'] as $key => $value){
                $objDir= new cls_File($value,$this->m_FtpDocRoot);
                $this->m_HtmlOutput .= '        <tr onMouseOver="this.style.backgroundColor=\'#ADE5F9\'" onMouseOut="this.style.backgroundColor=\'#FFFFFF\'">
                <td><img src="images/iconen/folder.png" alt="Mapje" width="16" height="16" /></td>
                <td>'
.$objDir->getDirLink($value).'</td>
                <td>map</td>
                <td>--</td>
                <td></td>
                <td>
                <img src="images/iconen/delete.png" width="16" height="16" border="0"/>
                <img src="images/iconen/textfield_rename.png" width="16" height="16" border="0"/>
                </td>
                </tr>'
."\n";
                $objDir=NULL;
            }
        }
else{
            $DirList = FALSE;
        }

        if(isset($aList['file'])){
            foreach($aList['file'] as $value){
                $objFile = new cls_File($value,$this->m_FtpDocRoot);
                    $this->m_HtmlOutput .= '    <tr onMouseOver="this.style.backgroundColor=\'#ADE5F9\'" onMouseOut="this.style.backgroundColor=\'#FFFFFF\'" >
                    <td><img src="images/iconen/'
.$objFile->getFileIcon($value['name']).'" alt="#" width="16" height="16" /></td>
                    <td>'
.$objFile->getFileName($value['name']).'</td>
                    <td>'
.$objFile->getFileType($value['name']).'</td>
                    <td>'
.$value['size'].'</td>
                    <td>'
.$value['lastmodi'].'</td>
                    <td width="170px">
                    </td>
                    </tr>'
."\n";
                    $objFile=NULL;
            }
        }
else{
            $FileList = FALSE;
        }

        if (isset($DirList) AND isset($FileList)){
        $this->m_HtmlOutput .='        
            <tr>
            <td>&nbsp;</td>
            <td><i>map is leeg. </i></td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            </tr >
            <tr>
            <td colspan="4"></td>
            <td align="right"></td>'
."\n";
}

            $this->m_HtmlOutput .='</tbody></table>';



            return $header.$this->m_HtmlOutput;
    }
// End function HtmlDirectoryListing

    public function CheckGivenPath($p_Path){
        $path='/';
        if(isset($_GET['SubDir'])){
            if(str_replace('.','NOTALOWED',$_GET['SubDir'])){
                $path .= str_replace('.','WRONG_PATH_GIVEN!!!!',$_GET['SubDir']);
            }
else{
                $path .= $_GET['SubDir'].'/';
            }
        }

    return $path;
    }
// End function CheckGivenPath()
}

//---------------------------------------------------------------------------------------------------
// Class name: cls_File
//---------------------------------------------------------------------------------------------------


Class cls_File{
    public $m_File;
    private $m_RootPath;
        private $am_FileTypes    = array(
    'png'    => array('page_white_picture.png', 'PNG Afbeelding'),
    'gif'    => array('page_white_picture.png', 'GIF Afbeelding'),
    'html'    => array('page_world.png', 'HTML Bestand'),
    'htm'    => array('page_white_world.png', 'HTML Bestand'),
    'jpeg'    => array('picture.png', 'JPG Afbeelding'),
    'jpg'    => array('picture.png', 'JPG Afbeelding'),
    'ico'    => array('page_white_picture.png', 'BMP Afbeelding'),
    'bmp'    => array('page_white_picture.png', 'BMP Afbeelding'),
    'zip'    => array('compress.png', 'Archief'),
    'rar'    => array('compress.png', 'Archief'),
    'tar'    => array('compress.png', 'Archief'),
    'gz'    => array('compress.png', 'Archief'),
    '7z'    => array('compress.png', 'Archief'),
    'swf'    => array('page_white_flash.png', 'Flash Movie'),
    'css'    => array('page_white_code.png', 'CSS Bestand'),
    'xml'    => array('page_white_code.png', 'XML Bestand'),
    'js'    => array('page_white_code_red.png', 'JavaScript Bestand'),
    'txt'    => array('page_white_text.png', 'Text Bestand'),
    'php'    => array('page_white_php.png', 'PHP Bestand'),
    'doc'    => array('page_word.png', 'MS Word Bestand'),
    'xls'    => array('page_excel.png', 'MS Excel Bestand'),
    'ppt'    => array('page_white_powerpoint.png', 'MS PowerPoint Bestand'),
    'dot'    => array('page_white_office.png', 'MS Office Bestand'),
    'pub'    => array('page_white_office.png', 'MS Publisher Bestand'),
    'vsd'    => array('page_white_office.png', 'MS Visio Bestand'),
    'mdb'    => array('page_white_database.png', 'MS Access Bestand'),
    'pdf'    => array('page_white_acrobat.png', 'Adobe Bestand'),
    'psd'    => array('page_white_acrobat.png', 'Adobe Bestand'),
    'esp'    => array('page_white_acrobat.png', 'Adobe Bestand'),
    'mp3'    => array('music.png', 'Geluid Bestand'),
    'wma'    => array('music.png', 'MS Media Bestand'),
    'wav'    => array('music.png', 'Geluid Bestand'),
    'exe'    => array('application_xp_terminal.png', 'Executable'),
    'bat'    => array('application_xp_terminal.png', 'Executable'),
    'cmd'    => array('application_xp_terminal.png', 'Executable'),
    'com'    => array('application_xp_terminal.png', 'Executable'),
    'ini'    => array('page_white_gear.png', 'Instellings Bestand'),
    'as'    => array('page_white_actionscript.png', 'Flash Actionsctipt'),
    'odf'    => array('oomath.png', 'OO Formule Bestand'),
    'odp'    => array('oopresenter.png', 'OO Presentatie Bestand'),
    'odt'    => array('ooowriter.png', 'OO Tekst Bestand'),
    'ods'    => array('oospreadsheet.png', 'OO Werkblad Bestand'),
    'odg'    => array('oodraw.png', 'OO Tekening Bestand '),
    'otg'    => array('oo2.png', 'OO Sjabloon Bestand'),
    'default'    => array('page_white.png', 'Onbekend Bestand'),
);
    function
__construct($ap_File,$p_RootPath=""){
        $this->m_RootPath = $p_RootPath;
        $m_File = $ap_File;
    }
// End function __construct()

    private function getaFileType($p_File){
        $FileExtensie = strstr($p_File, ".") ? strtolower(substr($p_File, 1+strrpos($p_File, "."))) : '';
        // Controleerd of extentie bekend is anders default.
        $Type = isset($this->am_FileTypes[$FileExtensie]) ? $this->am_FileTypes[$FileExtensie] : $this->am_FileTypes['default'];
        return $Type;
    }
// End function getFileType()
    
    public function getFileType($p_File){
        $aFileDescription = $this->getaFileType($p_File);
        return $aFileDescription[1];
    }
// End function getFileType()

    public function getFileIcon($p_File){
        $aFileDescription = $this->getaFileType($p_File);
        return $aFileDescription[0];
    }
// End function getFileIcon()

    private function strstr_after($p_File) {
        $p_aFile = explode('/', $p_File);
        $aFile =count($p_aFile);
        return $p_aFile[$aFile -1];
    }
// End function strstr_after()


    public function getFileName($p_File){
        return $this->strstr_after($p_File, '/');
    }
// End function getFileName()


    public function RenameFile($p_OldFileName, $p_NewFileNew){
        $ftp = new cls_ftp;
        if($ftp->RenameDirFile($p_OldFileName, $p_NewFileNew)){
            return TRUE;
        }
else{
            return FALSE;
        }

        $ftp=NULL;
    }
// End function renameFile(){}

    public function getFileDate(){
    }
// End function getFileDate()
    
    public function MoveFile($_OldPath, $p_NewPath){
    }
// End function MoveFile()
    
    public function DeleteFile(){
    }
// End function DeleteFile()
    
    public function getFileOptions(){
    }
// End function getFileOptions()
    
    public function getDirLink($p_DirName){
        $subdir= trim(str_replace($this->m_RootPath,'',$p_DirName),'/');
        $Html ='<a href="'.$_SERVER['PHP_SELF'].'?SubDir='.$subdir.'/"> '.$this->getFileName($p_DirName).'</a>';
        return $Html;
    }
// End function getDirLink()

    public function __destruct(){
    }
// End function __destruct()


}


$path='/';
if(isset($_GET['SubDir'])){
    if(str_replace('.','NOTALOWED',$_GET['SubDir'])){
        $path .= str_replace('.','WRONG_PATH_GIVEN!!!!',$_GET['SubDir']);
    }
else{
        $path .= $_GET['SubDir'].'/';
    }
}


echo $path;
$ftpObj=new DirectoryListing;
//$path = $ftpObj->CheckGivenPath((isset($_GET['SubDir']))?$_GET['SubDir']:'');
print $ftpObj->HtmlDirectoryListing($path);
$ftpObj->Disconnect();


?>
Gewijzigd op 01/01/1970 01:00:00 door Andreas Warnaar
 
PHP hulp

PHP hulp

11/04/2021 02:30:55
 
Jan geen

Jan geen

21/04/2008 21:27:00
Quote Anchor link
Ziet er wel aardig uit, misschien zou je bij de constructor al de username, pass en server kunnen meegeven. Verder nog wat conventies: laat dat cls_ maar weg. verder begin ikzelf methoden altijd met een kleine letter. Jij doet het soms wel soms niet, consistent zijn is het belangrijkst. Ben zelf ook niet zo van HTML in je classes, je kan dan beter een array teruggeven.
 
Andreas Warnaar

Andreas Warnaar

21/04/2008 21:34:00
Quote Anchor link
@Rik:Maak je dan een apparte klassen voor HTML ?
of verwerk je dat aleen in een script?
mod-edit::
Teveel code
Plaats geen hele scripts, maar snippets ter ondersteuning van je vraag. Ga er niet van uit dat leden scripts van meer dan 30 regels gaan lezen
Gewijzigd op 01/01/1970 01:00:00 door Andreas Warnaar
 
Jan geen

Jan geen

21/04/2008 21:54:00
Quote Anchor link
Dat verwerk je dan in een script
 



Overzicht Reageren

 
 

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.