upload.php File Reference

Functions for handling file uploads / file attachments. More...

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


Detailed Description

Functions for handling file uploads / file attachments.

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.


Function Documentation

delete_file ( item,
id,
filename 
)

delete an attached file

Parameters:
$item -- item type
$id -- item id
$filename -- the file name
Returns:
nothing

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

Parameters:
$item -- item type
$id -- item id
$filename -- the file name
Returns:
contents of the file

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

Parameters:
$item -- item type
$id -- item id
Returns:
an associative array that contains information about the attached files, as provided by the PHP function stat()

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

Parameters:
$item -- item type
$id -- item id
$filename -- the file name
$contents -- the file contents
Returns:
nothing

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.

Parameters:
$smarty -- Smarty template engine handle
$db -- MySQL database handle
$INPUT['item'] -- item type
$INPUT['id'] -- item id
Note:
This function uses the global variable $_FILES to retrieve the files that have been uploaded by the user.
Returns:
nothing

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:


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