Creating a mini-file-manager in PHP

Lets have a look on codes that i have written in php , html and styled in css combindly just in one file.


  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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
<html><title>File Manager by HS</title>
<head>
<link rel="stylesheet" href="js/vi.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>
<script type="text/javascript" src="js/scripts/shCore.js"></script>
<script src='js/jquery-ui.js' type="text/javascript"></script>
<link rel="stylesheet" href="js/styles/shCoreDefault.css">
<script type="text/javascript">SyntaxHighlighter.all();</script>
<?php


//fucntion for counting no. of folders and files in given directory
function f_D_counter($dir)
         {
            
            if (is_dir($dir)==1) 
            {
              $folder_count=0;
              $file_count=0;
                $open=opendir($dir);
                while ( $read= readdir($open)) 
                   {
                  if($read!='.'&&$read!='..')
                      {
                    $n=$dir."/".$read;
                    if (is_dir($n)==1) 
                    {
                      $folder_count++;
                      f_D_counter($n);
                    }
                    else
                    {   
                      $file_count++;
                      

                    }
                     }  
                   }
                   echo"fi".$file_count."fi";
                   echo"fol".$folder_count."fol<br>"; 
            }

         }
         if (!empty($_GET['dir'])) {
               f_D_counter($_GET['dir']);
                
                     }
//function for counting folder in fiven directory                      
function count_folder($dir)
    {
preg_match_all('/fol[0-9]fol/',file_get_contents("http://learnphpbyself.hostei.com/count_folder.php?dir=$dir"),$file);
$fol=implode("", $file[0]);
preg_match_all('/[0-9]/',$fol,$total);
$x=0;
for($i=0; $i<count($total[0]); $i++)
{
    $x=$x+$total[0][$i];
}
return $x;
    }
//function for counting files in given directory
function count_file($dir)
{
    preg_match_all('/fi[0-9]fi/',file_get_contents("http://learnphpbyself.hostei.com/count_folder.php?dir=$dir"),$file);
$fol=implode("", $file[0]);
preg_match_all('/[0-9]/',$fol,$total);
$x=0;
for($i=0; $i<count($total[0]); $i++)
{
    $x=$x+$total[0][$i];
}
return $x;
}
function jsonData($dir)
{
   $data=array();
   if(is_dir($dir)) 
   {
       $open=opendir($dir);
       while ( $read=readdir($open)) 
       {
           if ($read!='.'&&$read!='..') 
           {
              $loc=$dir.'/'.$read;
              array_push($data,$loc);
           }
       }
       $jdata=json_encode($data);
       $fopen=fopen("Data.json","w++");
       $fread=fputs($fopen, $jdata);
       copy("Data.json",$dir.'/Data.json');
   }
}       
function file_type($file)
{
   if (file_exists($file)) 
  {     
       $name=basename($file);
      
       if (preg_match('/./', $name)) 
       {
          $na=explode('.',$name);
          return  $na[count($na)-1];
  
      }
}
}
function delete_dir($path)
 {
  
 }
function exploreProject($fd)
{
  $filety='';
if(!empty($fd))
{
  if (is_dir($fd)) {
    
  
$read=opendir($fd);
while ($open=readdir($read)) 
{
  if($open!='.'&&$open!='..')
  {
  $rr=count_folder($open);
  $ss=count_file($open);
  if(is_file($open)){$filety='(File)';}else{$filety='(Folder)';}
  $srvr="http://learnphpbyself.hostei.com?path=".$fd."/".$open;
  echo "<div id='hs' title='$rr folders,$ss files'><a href='$srvr'>$srvr</a><br><k style='color:white; background:black;'>".$open.' '.$filety."</k></div>";
    }
}
 }

elseif (is_file($fd)) {
  
  ?>

<div id='hss' title=' 0 folders,1 file'>
   
   <div id='embd'>
    <ul class="lin"><div>
    setting<button id="rerun"style='margin-left:20p;'></button>
    <button id="select"></button>
  </div>
  <ul>
    <li><a href="http://learnphpbyself.hostei.com/?rfile=<?php echo $fd; ?>">See source File</a></li>
    <li><a href="http://learnphpbyself.hostei.com/<?php echo $fd; ?>">Run File</a></li>
    <li><a href="http://learnphpbyself.hostei.com/?rfile=<?php echo $fd.'#tabs-3';?>">Edit File</a></li>
    <li><a href="http://learnphpbyself.hostei.com/?dfile=<?php echo $fd; ?>">Delete File</a></li>
  </ul>
    </ul>
   </div>
   <div id='maind'>

    <a href='http://learnphpbyself.hostei.com/?path=<?php echo $fd; ?>'><?php echo $fd; ?></a>
   </div>
</div>
<?php
}
else
 {
  "<h1>Whoops!! Something Went Wrong..</h1>";
 }
} 
}
function see_file($file)
{
  if (file_exists($file)) 
  {     
       $name=basename($file);
       if (preg_match('/./', $name)) 
       {
          $na=explode('.',$name);
          switch ($na[count($na)-1]) 
          {
            case 'css' || 'CSS':
            {
              echo"<script src='js/scripts/shBrushCss.js'></script>";
              echo"<link type='text/css' rel='stylesheet' href='js/scripts/styles/shCoreDefault.css'>";   
              echo "<pre style='baackground:white;' class='brush:css;'>"; 
               
            }
            break;
            case 'js' || 'JS':
            {
              echo"<script src='js/scripts/shBrushJScript.js'></script>";
                echo"<link type='text/css' rel='stylesheet' href='js/scripts/styles/shCoreDefault.css'>";   
              echo "<pre style='baackground:white;' class='brush:js;'>";
               
            }
            break;
            case 'php' ||'inc':
            {
              echo"<script src='js/scripts/shBrushPhp.js'></script>";
                echo"<link type='text/css' rel='stylesheet' href='js/scripts/styles/shCoreDefault.css'>";   
              echo "<pre style='baackground:white;' class='brush: php;'>";
               
            }
            break;
            case 'sql':
            {
              echo"<script src='js/scripts/shBrushSql.js'></script>";
                echo"<link type='text/css' rel='stylesheet' href='js/scripts/styles/shCoreDefault.css'>";   
              echo "<pre style='baackground:white;' class='brush: sql;'>";
               
            }
            break;
            default:
              
              break;
          }

       }
       $fopen=fopen($file,"r");
       
       while ($fread=fread($fopen,13421772)) 
       {
        echo htmlspecialchars($fread);
       }echo'</pre>';
  }
}
function fetch_file($file)
{
  if (file_exists($file)) 
  { 
   $fopen=fopen($file,"r");
       
       while ($fread=fread($fopen,13421772)) 
       {
        echo $fread;
       }
   }
}      

?>

<script type="text/javascript">
  $(function() {
    $( document ).tooltip({
      track: true
    });
  
    $( "#tabs" ).tabs();
    $( "#editTab" ).tabs();
  });
</script>
<script type="text/javascript">
    $(function() {
  var availableTags =<?php if(!empty($_GET['path'])){if(is_dir($_GET['path'])){echo file_get_Contents('http://learnphpbyself.hostei.com/'.$_GET['path'].'/Data.json');}}?>;
    $( ".srchi" ).autocomplete({
      source: availableTags
    });});
</script>
<script>
  $(function() {
    $( "#rerun" )
      .button({
        text: false,
          icons: {
            primary: "ui-icon-gear"
          }

      })
      .click(function() {
        
      })
      .next()
        .button({
          text: false,
          icons: {
            primary: "ui-icon-triangle-1-s"
          }
        })
        .click(function() {
          var menu = $( this ).parent().next().show().position({
            my: "left top",
            at: "left bottom",
            of: this
          });
          $( document ).one( "click", function() {
            menu.hide();
          });
          return false;
        })
        .parent()
          .buttonset()
          .next()
            .hide()
            .menu();
  });
  </script>
</head>
<body>
<div id="tabs">
  <ul>
    <li><a href="#tabs-1">File Explorer</a></li>
    <li><a href="#tabs-2">CREATE PROJECTS</a></li>
    <li><a href="#tabs-3">EXPLORER 2</a></li>
    <li><a href="#tabs-4">ABOUT FILE MANAGER</a></li>
  </ul>
  <div id="tabs-1">
  PROJECTS
<ul class="headli">
    <form class='srch'action="<?php $_SERVER["PHP_SELF"];?>" method='GET'>
    PATH=<li class="headlis"><input class='srchi' type="text" name="path"/></li>
    <li class="headlis"><input class='srchs' type="submit" name='submit1' value="SEARCH"/></li>
        </form>   
</ul>
  
    <?php 
    if (!empty($_GET['dfile'])) {
        delete_dir($_GET['dfile']);
    }
  if (!empty($_GET['rfile'])&&empty($_GET['sfile'])&&empty($_GET['path'])) {
      echo"<a style='color:Cyan;font-size:30px' href='http://learnphpbyself.hostei.com?path=".$_GET['rfile']."'>Back</a>({$_GET['rfile']})<br>";
      see_file($_GET['rfile']);
    }
    else{
  if (empty($_GET['sfile'])) {
    
  
    if (!empty($_GET['path'])&&empty($_GET['cpath'])) {
      jsonData($_GET['path']);
      echo"<a style='color:Cyan;font-size:30px' href='http://learnphpbyself.hostei.com?path={$_GET['path']}/../'>Back</a>({$_GET['path']})<br>";
      exploreProject($_GET['path']);
    }
   
    else
    {
      exploreProject(".");
    }}
    
    elseif(!empty($_GET['sfile'])){
      echo"<div id='edt'><input type='submit' value='Edit File'/></div>";
      see_file($_GET['sfile']);
    }}
    
  ?>
  
    </div>
  <div id="tabs-2">
    CREATE PROJECTS
    <ul class="headli">CREATE FOLDER
    <form class='srch'action="<?php echo $_SERVER["PHP_SELF"].'#tabs-2';?>" method='GET'>
    <?php if (!empty($_GET['path'])){if(is_dir($_GET['path'])){echo $_GET['path'].'/';} };?><li class="headlis">
        <li class="headlis">
        <input type="hidden" name="path" value="<?php if (!empty($_GET['path'])){if(is_dir($_GET['path'])){echo$_GET['path'];}}?>"/>
    <input class='csrchi' type="text" name="cfile"/></li>
    
    <li class="headlis"><input class='srchs' type="submit" name='submit1' value="CREATE"/></li>
        </form>   
        </ul><ul class="headli">CREATE FILE
    <form class='srch'action="<?php echo $_SERVER["PHP_SELF"].'#tabs-2';?>" method='GET'>
    <?php if (!empty($_GET['path'])){if(is_dir($_GET['path'])){echo $_GET['path'].'/';} };?><li class="headlis">
        <li class="headlis">
        <input type="hidden" name="path" value="<?php if (!empty($_GET['path'])){if(is_dir($_GET['path'])){echo$_GET['path'];}}?>"/>
    <input class='csrchi' type="text" name="cfile"/></li>
    
    <li class="headlis"><input class='srchs' type="submit" name='submit2' value="CREATE"/></li>
        </form>   
        </ul>
        <?php
         if (!empty($_GET['path'])&&is_dir($_GET['path'])&&!empty($_GET['cfile'])) 
{          
  if (!file_exists($_GET['path'].'/'.$_GET['cfile'])) 
  {
       if (!empty($_GET['submit2'])) 
       {
               $pth=$_GET['path'].'/'.$_GET['cfile'];
               $cfile=touch($_GET['path'].'/'.$_GET['cfile']);
               if ($cfile==true) 
               {
                    echo "Project Created.<a href='http://learnphpbyself.hostei.com?path={$pth}'>(Open)</a>";
               }
               else
               {
                echo "Problem Occured!!";
               }
       }
       elseif (!empty($_GET['submit1'])) 
       {    
            $pth=$_GET['path'].'/'.$_GET['cfile'];
            $cfile=mkdir($_GET['path'].'/'.$_GET['cfile']);
            if ($cfile==true) 
               {
                    echo "Project Created.<a href='http://learnphpbyself.hostei.com?path={$pth}'>(Open)</a>";
               }
               else
               {
                echo "Problem Occured!!";
               }
       }
  }
  else
  {
     echo "File Already Exists <a hrf='http://learnphpbyself.hostei.com?path={$_GET['path']}'> {$_GET['path']}</a>";
  }
}          
        ?>
    </div>
  <div id="tabs-3">
    EXPLORER 2<br>
    <p>Say Hello To <a href="http://hiteshs.netai.net">HITESH SAINI</a></p>
    <?php
    if (!empty($_GET['rfile'])&&empty($_GET['sfile'])&&empty($_GET['path'])) 
       {
      ?>
      <form action="<?php echo $_SERVER['PHP_SELF'];?>" method='POST'>
        <input type="hidden" name='rfile' value='<?php echo $_GET['rfile'];?>'/>
        
        <input type="submit" value="SAVE EDIT" name="EDITFILE"/><br>
        <textarea id='highlightit' style='height:700px;width:700px;' class='<?php echo 'brush:'.file_type($_GET['rfile']);?>' name="textforEdit">
          <?php fetch_file($_GET['rfile']); ?>
            
        </textarea><br>
        
        <input type="submit" value="SAVE EDIT" name="EDITFILE"/>
      </form>
    <?php 
      
    }
  ?>
  <?php 
            if (!empty($_POST['EDITFILE'])&&!empty($_POST['textforEdit'])&&!empty($_POST['rfile'])) 
            {
                $baseIname=basename($_POST['rfile']);
                if($baseIname=='index.php'||$baseIname=='vi.css')
                {  
                 echo "Can't Be edit!";
                } 
                else{
                $fope=fopen($_POST['rfile'],"w++");
                $fre=fputs($fope, $_POST['textforEdit']);
                fclose($fope);
                $location='http://learnphpbyself.hostei.com?path='.$_POST['rfile'];
                echo"<a href='$location'>See Edited File</a>";
                }
            }
             ?></div>
<div id="tabs-4">ABOUT FILE MANAGER<br><br>
        This software is built in PHP, jquery ,jquery ui, css, and html and used for file management on 
        server. And further it can be used by users for running php scripts directly without setting up any 
        configuration and installing any other software in working machine.<br><br> 
       Why to use this software?
       <br>-this software can be used to run PHP codes through it without setting up any configuration
       and integration.<br><br>
      <p style='text-decoration:underline'> HOW TO </p>
       <p style='text-decoration:underline'>To see a project and files</p>
       -To see a project and files click on the directory which are rendered on screen.
       then it will redirect you to the project's page.
       <br>
       <p style='text-decoration:underline'>To create a new folder and file</p>
       To create a file and folder, first go to that directory as described earlier
       in which a folder or file  are to be created. then Go to the second main tab 
       having title 'Create projects' and give name to folder or to file by filling the first or 
       second input. and then click on the create button. thus new file or folder will be created in that directory.<br><br>
       <p style='text-decoration:underline'>To edit a Existing file or project</p>
       To Edit a existing file or project  go to the targret file and click on the drop down icon and click on the edit link
       then page will redirect you to the edit page of that file or project. after editing the file click on save button. 
       editing will be saved to actual file. then you can run the edited file or project.
       <p style='text-decoration:underline'>For more help</p> you can contact me on my site <a href="http://hiteshs.netai.net">here</a>
       and my fb profile <a href="https://facebook.com/HiteshSaini99">here</a>
       
        </div>
             </div></div>
<div id='ps'></div>
</body>
</html>

<?php
if(!empty($_GET["fileo"]))
  {
        
    
  }
?>

See Codes On Github

Comments

Popular posts from this blog

ciphers continued--Vegerne Cipher and its application in php

Message Sending API (Unofficial) for Way2SMS [ PHP ]

Working with Frameworks of PHP- The MVC structure