Files Handler


See also:
Documentation: FilesHandlerInfo.
This is the development page for the Files handler.
 


This is another a complete refactoring of the FilesManagementHandler I proposed previously. The new overview can be found at FilesManagementSolution

Dependancy

None.

The code

to be saved as files.php in the handlers/page directory.

  1. <div class="page">
  2. <?php
  3. if (! function_exists('mkdir_r')) {
  4.         function mkdir_r ($dir) {
  5.                 if (strlen($dir) == 0) return 0;
  6.                 if (is_dir($dir)) return 1;
  7.                 elseif (dirname($dir) == $dir) return 1;
  8.                 return (mkdir_r(dirname($dir)) and mkdir($dir,0755));
  9.         }
  10. }
  11.  
  12. if ($download <> '') {
  13.         // link to download a file
  14.         if ($text == '') $text = $download;
  15.         echo "<a href=\"".$this->href('files.xml',$this->GetPageTag(),'action=download&file='.urlencode($download))."\">".$text."</a>";
  16.  
  17. } elseif ($this->page AND ($this->method <> 'print.xml') AND ($this->method <> 'edit')) {
  18.  
  19.         // upload path
  20.         if ($this->config['upload_path'] == '') $this->config['upload_path'] = 'files';
  21.         $upload_path = $this->config['upload_path'].'/'.$this->GetPageTag();
  22.         if (! is_dir($upload_path)) mkdir_r($upload_path);
  23.  
  24.         // upload action
  25.         $uploaded = $_FILES['file'];
  26.         if ($_REQUEST['action'] == 'upload' AND $uploaded['size'] > 0)
  27.                 copy ($uploaded['tmp_name'], $upload_path.'/'.$uploaded['name']);
  28.  
  29.         // form
  30.         $result = "<form action=\"".$this->href()."/files\" method=\"post\" enctype=\"multipart/form-data\">\n";
  31.         if (!$this->config["rewrite_mode"]) $result .= "<input type=\"hidden\" name=\"wakka\" value=\"".$this->MiniHref()."/files\">\n";
  32.         echo $result;
  33.         // only if the user can write
  34.         if ($this->HasAccess('write'))
  35.         {
  36.             ?>
  37.             <input type="hidden" name="action" value="upload"><input type="file" name="file"><input type="submit" value="+">
  38.             <?php
  39.         }
  40.         echo "<a href=\"".$this->Href()."\">Back to the page</a>";
  41.         echo $this->FormClose();
  42.  
  43.         // uploaded files
  44.         $dir = opendir($upload_path);
  45.         while ($file = readdir($dir)) {
  46.                 if ($file != '.' && $file != '..') {
  47.                         // only if the user can write
  48.                         if ($this->HasAccess('write'))
  49.                         {
  50.                             $delete_link = "<a href=\"".$this->href('files.xml',$this->GetPageTag(),'action=delete&file='.urlencode($file))."\">x</a>";
  51.                         }
  52.                         else
  53.                         {
  54.                             $delete_link = "-";
  55.                         }
  56.                         $download_link = "<a href=\"".$this->href('files.xml',$this->GetPageTag(),'action=download&file='.urlencode($file))."\">".$file."</a>";
  57.                         print "[ {$delete_link} ] ";
  58.                         if ($file == $uploaded['name'])
  59.                                 print "<em>{$download_link}</em>\n";
  60.                         else
  61.                                 print $download_link;
  62.                         print '<br>';
  63.                 }
  64.         }
  65.         closedir($dir);
  66. }
  67. ?>
  68. </div>


To Do

More comments in the code - comments standardization...


CategoryDevelopmentHandlers
There are 4 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki