upload.php

Go to the documentation of this file.
00001 <?php
00008 
00009 require_once("error.php");
00010 require_once 'const.php';
00011 require_once("config.php");
00012 
00019 
00020 function list_files($item, $id) {
00021         global $upload_base_dir;
00022 
00023         $item = basename($item);
00024         $id = basename($id);
00025         $dir = "$upload_base_dir/$item/$id"; 
00026 
00027         $answer = array();
00028 
00029         if (!($handle = opendir($dir))){
00030                 return $answer;
00031         }
00032 
00033         while (false !== ($file = readdir($handle))) {
00034                 if (is_file("$dir/$file")) {
00035                         $answer[$file] = stat("$dir/$file");
00036                 }
00037         }
00038         closedir($handle);
00039         ksort($answer);
00040 
00041         return $answer; 
00042 }
00043 
00050 
00051 function delete_file($item, $id, $filename) {
00052         global $upload_base_dir;
00053 
00054         $item = basename($item);
00055         $id = basename($id);
00056         $filename = basename($filename);
00057 
00058         if (!unlink("$upload_base_dir/$item/$id/$filename")) {
00059                 user_error("could not delete file '$item/$id/$filename", 
00060                         E_USER_ERROR);  
00061         }
00062 }
00063 
00064 
00071 
00072 function get_file($item, $id, $filename) {
00073         global $upload_base_dir;
00074 
00075         $item = basename($item);
00076         $id = basename($id);
00077         $filename = basename($filename);
00078 
00079         return file_get_contents("$upload_base_dir/$item/$id/$filename");
00080 }
00081 
00089 
00090 function put_file($item, $id, $filename, $contents) {
00091         global $upload_base_dir;
00092 
00093         $item = basename($item);
00094         $id = basename($id);
00095         $filename = basename($filename);
00096 
00097         if (!is_dir("$upload_base_dir/$item")) {
00098                 mkdir("$upload_base_dir/$item") or 
00099                 user_error("mkdir '$item' failed", E_USER_ERROR); 
00100         }
00101 
00102         if (!is_dir("$upload_base_dir/$item/$id")) {
00103                 mkdir("$upload_base_dir/$item/$id") or 
00104                 user_error("mkdir '$item/$id' failed", E_USER_ERROR); 
00105         }
00106 
00107         $file = "$upload_base_dir/$item/$id/$filename";
00108 
00109         if (!($handle = fopen($file, "wb"))){
00110                 user_error("could not open file '$file'", E_USER_ERROR); 
00111         }
00112 
00113         $c = fwrite($handle,$contents, strlen($contents));
00114         fclose($handle);
00115 
00116         if ($c != strlen($contents)) {
00117                 user_error("could not write to file '$item/$id/$filename'", 
00118                         E_USER_ERROR);  
00119         }
00120 }
00121 
00135 
00136 function upload($smarty, $INPUT, $db) {
00137         global $_FILES;
00138 
00139         $display_html_form = TRUE;
00140 
00141 
00142         if (isset($INPUT['b_ok'])) {
00143 
00144                 foreach ($_FILES as $f) {
00145 
00146                         if (!is_uploaded_file($f['tmp_name'])) {
00147                                 continue;
00148                         }
00149 
00150                         $c = file_get_contents($f['tmp_name']); 
00151                         $fn = basename($f['name']);
00152                         put_file($INPUT['item'], $INPUT['id'], $fn, $c);
00153                         $display_html_form = FALSE;
00154                 }
00155         }
00156 
00157         if ($display_html_form == TRUE) {
00158                 $tpl_vars = $INPUT;
00159                 do_template($smarty, 'upload.tpl', $tpl_vars);
00160                 exit(0);
00161         }
00162 }
00163 
00164 ?>

Generated on Fri Jul 14 17:38:55 2006 for semapp by  doxygen 1.4.7