Files Handler
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.
<div class="page">
<?php
� � � � function mkdir_r ($dir) {
� � � � � � � �
if (strlen($dir) ==
0) return 0;
� � � � � � � �
if (is_dir($dir)) return 1;
� � � � � � � �
elseif (dirname($dir) ==
$dir) return 1;
� � � � � � � �
return (mkdir_r
(dirname($dir)) and
mkdir($dir,
0755));
� � � � }
}
�
if ($download <> '') {
� � � � // link to download a file
� � � � if ($text == '') $text = $download;
� � � �
echo "<a href=\"".
$this->
href('files.xml',
$this->
GetPageTag(),
'action=download&file='.urlencode
($download)).
"\">".
$text.
"</a>";
�
} elseif ($this->page AND ($this->method <> 'print.xml') AND ($this->method <> 'edit')) {
�
� � � � // upload path
� � � � if ($this->config['upload_path'] == '') $this->config['upload_path'] = 'files';
� � � � $upload_path = $this->config['upload_path'].'/'.$this->GetPageTag();
� � � �
if (!
is_dir($upload_path)) mkdir_r
($upload_path);
�
� � � � // upload action
� � � � $uploaded = $_FILES['file'];
� � � � if ($_REQUEST['action'] == 'upload' AND $uploaded['size'] > 0)
� � � � � � � �
copy ($uploaded['tmp_name'],
$upload_path.
'/'.
$uploaded['name']);
�
� � � � // form
� � � � $result = "<form action=\"".$this->href()."/files\" method=\"post\" enctype=\"multipart/form-data\">\n";
� � � � if (!$this->config["rewrite_mode"]) $result .= "<input type=\"hidden\" name=\"wakka\" value=\"".$this->MiniHref()."/files\">\n";
� � � � // only if the user can write
� � � � if ($this->HasAccess('write'))
� � � � {
� � � � � � ?>
� � � � � � <input type="hidden" name="action" value="upload"><input type="file" name="file"><input type="submit" value="+">
� � � � � � <?php
� � � � }
� � � �
echo "<a href=\"".
$this->
Href().
"\">Back to the page</a>";
� � � �
echo $this->
FormClose();
�
� � � � // uploaded files
� � � �
$dir =
opendir($upload_path);
� � � �
while ($file =
readdir($dir)) {
� � � � � � � � if ($file != '.' && $file != '..') {
� � � � � � � � � � � � // only if the user can write
� � � � � � � � � � � � if ($this->HasAccess('write'))
� � � � � � � � � � � � {
� � � � � � � � � � � � � � $delete_link = "<a href=\"".$this->href('files.xml',$this->GetPageTag(),'action=delete&file='.urlencode($file))."\">x</a>";
� � � � � � � � � � � � }
� � � � � � � � � � � � else
� � � � � � � � � � � � {
� � � � � � � � � � � � � � $delete_link = "-";
� � � � � � � � � � � � }
� � � � � � � � � � � � $download_link = "<a href=\"".$this->href('files.xml',$this->GetPageTag(),'action=download&file='.urlencode($file))."\">".$file."</a>";
� � � � � � � � � � � �
print "[ {$delete_link} ] ";
� � � � � � � � � � � � if ($file == $uploaded['name'])
� � � � � � � � � � � � � � � �
print "<em>{$download_link}</em>\n";
� � � � � � � � � � � � else
� � � � � � � � � � � � � � � �
print $download_link;
� � � � � � � � � � � �
print '<br>';
� � � � � � � � }
� � � � }
}
?>
</div>
To Do
More comments in the code - comments standardization...
CategoryDevelopmentHandlers