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
Comments
Comment by HSI-KBW-082-212-026-038.hsi.kabelbw.de
2005-02-26 20:21:18
You should use move_uploaded_file() instead of copy(), copy() doesn't work on all systems (on mine for example it didn't).
Comment by TimoK
2005-02-26 20:22:05
Above comment was from me, forgot to log in.
Comment by PeeJay
2005-07-14 10:34:27
For us poor souls who must use the Micro$oft IIS web server to host our Wikka Wakka Wiki, I suggest changing the line:
if ($file != '.' && $file != '..') {
to
if ($file != '.' && $file != '..' && $file != '_vti_cnf') {

as the web server adds the '_vti_cnf' directory automatically, which means that this directory gets shown in the file list each time.
Comment by JavaWoman
2005-07-15 11:46:15
Since the procedure is meant to read *files* and not directories (let alone recurse through them), it would be better to check for files only, rather than summing up whatever directory may need to be skipped.

So instead of:
if ($file != '.' && $file != '..')
the condition would become:
if (is_file($file))
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki