Go to the source code of this file.
Functions | |
| list_files ($item, $id) | |
| list files attached to an item | |
| delete_file ($item, $id, $filename) | |
| delete an attached file | |
| get_file ($item, $id, $filename) | |
| retrieve the contents of an attached file | |
| put_file ($item, $id, $filename, $contents) | |
| create an attachment | |
| upload ($smarty, $INPUT, $db) | |
| handle file uploads | |
It is possible to attach one or more files to an item. For example, you attach an online Table of Contents to a book. These functions manage the storage and retrieval of these files.
Definition in file upload.php.
| delete_file | ( | $ | item, | |
| $ | id, | |||
| $ | filename | |||
| ) |
delete an attached file
| $item | -- item type | |
| $id | -- item id | |
| $filename | -- the file name |
Definition at line 51 of file upload.php.
Referenced by del_item(), and do_delete_item().
00051 { 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 }
| get_file | ( | $ | item, | |
| $ | id, | |||
| $ | filename | |||
| ) |
retrieve the contents of an attached file
| $item | -- item type | |
| $id | -- item id | |
| $filename | -- the file name |
Definition at line 72 of file upload.php.
Referenced by view_item().
00072 { 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 }
| list_files | ( | $ | item, | |
| $ | id | |||
| ) |
list files attached to an item
| $item | -- item type | |
| $id | -- item id |
Definition at line 20 of file upload.php.
Referenced by do_delete_item(), edit_item(), show_collection(), and view_item().
00020 { 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 }
| put_file | ( | $ | item, | |
| $ | id, | |||
| $ | filename, | |||
| $ | contents | |||
| ) |
create an attachment
| $item | -- item type | |
| $id | -- item id | |
| $filename | -- the file name | |
| $contents | -- the file contents |
Definition at line 90 of file upload.php.
Referenced by edit_item(), main(), and upload().
00090 { 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 }
| upload | ( | $ | smarty, | |
| $ | INPUT, | |||
| $ | db | |||
| ) |
handle file uploads
This function is called by action.php. It provides a form where the user can upload attachments to documents.
| $smarty | -- Smarty template engine handle | |
| $db | -- MySQL database handle | |
| $INPUT['item'] | -- item type | |
| $INPUT['id'] | -- item id |
Definition at line 136 of file upload.php.
References do_template(), and put_file().
00136 { 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 }
Here is the call graph for this function:

1.4.7