fileupload.module

  1. drupal
    1. 4.6 developer/examples/fileupload.module
    2. 4.7 developer/examples/fileupload.module
    3. 5 developer/examples/fileupload.module

This is an example to demonstrate how to make a Drupal node support file uploads.

@Database definition:

  CREATE TABLE fileupload (
    nid int(10) unsigned NOT NULL default '0',
    filename varchar(255) not null,
    filepath varchar(255) not null,
    filemime varchar(255) not null,
    filesize int(10) unsigned not null
  );

Functions & methods

NameDescription
fileupload_accessImplementation of hook_access.
fileupload_deleteImplementation of hook_delete().
fileupload_file_downloadImplementation of hook_file_download.
fileupload_formImplementation of hook_form().
fileupload_helpImplementation of hook_help.
fileupload_insertImplementation of hook_insert().
fileupload_loadImplementation of hook_load().
fileupload_menuImplementation of hook_menu.
fileupload_node_infoImplementation of hook_node_info.
fileupload_permImplementation of hook_perm.
fileupload_submitImplementation of hook_submit().
fileupload_updateImplementation of hook_update().
fileupload_validateImplementation of hook_validate().
fileupload_viewImplementation of hook_view.

File

developer/examples/fileupload.module
View source
  1. /**
  2. * @file
  3. * This is an example to demonstrate how to make a Drupal node support file
  4. * uploads.
  5. *
  6. * @Database definition:
  7. * @code
  8. * CREATE TABLE fileupload (
  9. * nid int(10) unsigned NOT NULL default '0',
  10. * filename varchar(255) not null,
  11. * filepath varchar(255) not null,
  12. * filemime varchar(255) not null,
  13. * filesize int(10) unsigned not null
  14. * );
  15. * @endcode
  16. */
  17. /**
  18. * Implementation of hook_menu.
  19. */
  20. function fileupload_menu($may_cache) {
  21. $items = array();
  22. if ($may_cache) {
  23. $items[] = array('path' => 'node/add/fileupload', 'title' => t('file upload'),
  24. 'access' => user_access('create files'));
  25. }
  26. return $items;
  27. }
  28. /**
  29. * Implementation of hook_perm.
  30. */
  31. function fileupload_perm() {
  32. return array('create files');
  33. }
  34. /**
  35. * Implementation of hook_help.
  36. */
  37. function fileupload_help($section) {
  38. switch ($section) {
  39. case 'admin/modules#description':
  40. return t('An example module to demonstrate file uploads.');
  41. case 'node/add#fileupload':
  42. return t('Simple file upload node example.');
  43. }
  44. }
  45. /**
  46. * Implementation of hook_node_info.
  47. */
  48. function fileupload_node_info() {
  49. return array(
  50. 'fileupload' => array(
  51. 'name' => t('file upload'),
  52. 'module' => 'fileupload',
  53. 'description' => t('Example file upload content type'),
  54. 'has_title' => TRUE,
  55. 'has_body' => FALSE)
  56. );
  57. }
  58. /**
  59. * Implementation of hook_access.
  60. */
  61. function fileupload_access($op, $node) {
  62. global $user;
  63. switch ($op) {
  64. case 'create':
  65. return user_access("create file");
  66. case 'update':
  67. case 'delete':
  68. return user_access("create file") && ($user->uid == $node->uid);
  69. }
  70. }
  71. /**
  72. * Implementation of hook_file_download.
  73. */
  74. function fileupload_file_download($file) {
  75. if (user_access('access content')) {
  76. if ($filemime = db_result(db_query("SELECT filemime FROM {fileupload} WHERE filepath = '%s'", file_create_path($file)))) {
  77. return array('Content-type:' . $filemime);
  78. }
  79. }
  80. else {
  81. return -1;
  82. }
  83. }
  84. /**
  85. * Implementation of hook_view.
  86. */
  87. function fileupload_view(&$node, $teaser = FALSE, $page = FALSE) {
  88. $node = node_prepare($node, $teaser);
  89. if ($file = $node->file) {
  90. // Only create link to file when it is in the proper location.
  91. if ($file->filesize && file_check_location($file->filepath, file_directory_path())) {
  92. $node->content['body']['#value'] = l($file->filename, file_create_url($file->filepath));
  93. }
  94. else {
  95. $node->content['body']['#value'] = $file->filename;
  96. }
  97. // display the file's size
  98. $node->content['body']['#value'] .= ' ('. format_size($file->filesize) .')';
  99. }
  100. return $node;
  101. }
  102. /**
  103. * Implementation of hook_form().
  104. *
  105. * Return an array of the form elements needed to edit this node.
  106. */
  107. function fileupload_form(&$node) {
  108. // Set form parameters so we can accept file uploads.
  109. $form['#attributes'] = array('enctype' => 'multipart/form-data');
  110. // standard node title
  111. $form['title'] = array(
  112. '#type' => 'textfield',
  113. '#title' => t('Title'),
  114. '#size' => 60,
  115. '#maxlength' => 128,
  116. '#required' => TRUE,
  117. '#default_value' => $node->title
  118. );
  119. // file upload field
  120. $form['file'] = array(
  121. '#type' => 'file',
  122. '#title' => t('File'),
  123. '#size' => 40,
  124. '#default_value' => '',
  125. );
  126. if ($node->file->filepath) {
  127. $form['file']['#description'] .= t('A file already exists, if you upload another file the current file will be replaced.');
  128. }
  129. if ($node->nid) {
  130. // the validate function doesn't get a full copy of the node, only the
  131. // data posted from the form. use this field to tell the validator
  132. // the path of the node's current file.
  133. $form['current_file'] = array(
  134. '#type' => 'value',
  135. '#value' => $node->file->filepath
  136. );
  137. }
  138. return $form;
  139. }
  140. /**
  141. * Implementation of hook_validate().
  142. *
  143. * Verify that there is a file attached to the node.
  144. */
  145. function fileupload_validate(&$node) {
  146. // Check for a new file upload.
  147. if ($file = file_check_upload('file')) {
  148. $node->file = $file;
  149. }
  150. // Make sure there is an upload or an existing file.
  151. if (!$file && !file_exists($node->current_file)) {
  152. form_set_error('file', t('A file must be provided.'));
  153. }
  154. }
  155. /**
  156. * Implementation of hook_submit().
  157. *
  158. * If a file was uploaded save it before updating the database.
  159. */
  160. function fileupload_submit(&$node) {
  161. // if a file was uploaded, move it to the files directory
  162. if ($file = file_check_upload('file')) {
  163. $node->file = file_save_upload($file, file_directory_path(), false);
  164. }
  165. }
  166. /**
  167. * Implementation of hook_load().
  168. *
  169. * The node is being loaded. Load the file's information from the database so
  170. * it can be added to the node.
  171. */
  172. function fileupload_load($node) {
  173. $extras = new StdClass();
  174. $extras->file = db_fetch_object(db_query('SELECT filename, filepath, filemime, filesize FROM {fileupload} WHERE nid = %d', $node->nid));
  175. return $extras;
  176. }
  177. /**
  178. * Implementation of hook_insert().
  179. *
  180. * The node is being created, save the file's infomation to the database.
  181. */
  182. function fileupload_insert($node) {
  183. $file = $node->file;
  184. db_query("INSERT INTO {fileupload} (nid, filename, filepath, filemime, filesize) VALUES (%d, '%s', '%s', '%s', %d)", $node->nid, $file->filename, $file->filepath, $file->filemime, $file->filesize);
  185. }
  186. /**
  187. * Implementation of hook_update().
  188. *
  189. * The node is being updated, save changes to the file infomation to the database.
  190. */
  191. function fileupload_update($node) {
  192. $newfile = $node->file->filepath;
  193. $oldfile = db_result(db_query('SELECT filepath FROM {fileupload} WHERE nid = %d', $node->nid));
  194. // only make changes if there was a new file.
  195. if ($newfile) {
  196. if ($newfile != $oldfile) {
  197. // delete the old file
  198. file_delete($oldfile);
  199. }
  200. // update the database
  201. $file = $node->file;
  202. db_query("UPDATE {fileupload} SET filename = '%s', filepath = '%s', filemime = '%s', filesize = %d WHERE nid = %d", $file->filename, $file->filepath, $file->filemime, $file->filesize, $node->nid);
  203. }
  204. }
  205. /**
  206. * Implementation of hook_delete().
  207. *
  208. * The node is being delete, remove the file and any information.
  209. */
  210. function fileupload_delete($node) {
  211. file_delete($node->filepath);
  212. db_query("DELETE FROM {fileupload} WHERE nid = %d", $node->nid);
  213. }
Login or register to post comments