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_executeImplementation of hook_execute().
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_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('fileupload' => array('name' => t('file upload'), 'base' => 'fileupload'));
  50. }
  51. /**
  52. * Implementation of hook_access.
  53. */
  54. function fileupload_access($op, $node) {
  55. global $user;
  56. switch ($op) {
  57. case 'create':
  58. return user_access("create file");
  59. case 'update':
  60. case 'delete':
  61. return user_access("create file") && ($user->uid == $node->uid);
  62. }
  63. }
  64. /**
  65. * Implementation of hook_file_download.
  66. */
  67. function fileupload_file_download($file) {
  68. if (user_access('access content')) {
  69. if ($filemime = db_result(db_query("SELECT filemime FROM {fileupload} WHERE filepath = '%s'", file_create_path($file)))) {
  70. return array('Content-type:' . $filemime);
  71. }
  72. }
  73. else {
  74. return -1;
  75. }
  76. }
  77. /**
  78. * Implementation of hook_view.
  79. */
  80. function fileupload_view(&$node, $teaser = FALSE, $page = FALSE) {
  81. if ($file = $node->file) {
  82. // Only create link to file when it is in the proper location.
  83. if ($file->filesize && file_check_location($file->filepath, file_directory_path())) {
  84. $node->body = l($file->filename, file_create_url($file->filepath));
  85. }
  86. else {
  87. $node->body = $file->filename;
  88. }
  89. // display the file's size
  90. $node->body .= ' ('. format_size($file->filesize) .')';
  91. }
  92. return theme('node', $node, $teaser, $page);
  93. }
  94. /**
  95. * Implementation of hook_form().
  96. *
  97. * Return an array of the form elements needed to edit this node.
  98. */
  99. function fileupload_form(&$node) {
  100. // Set form parameters so we can accept file uploads.
  101. $form['#attributes'] = array('enctype' => 'multipart/form-data');
  102. // standard node title
  103. $form['title'] = array(
  104. '#type' => 'textfield',
  105. '#title' => t('Title'),
  106. '#size' => 60,
  107. '#maxlength' => 128,
  108. '#required' => TRUE,
  109. '#default_value' => $node->title
  110. );
  111. // file upload field
  112. $form['file'] = array(
  113. '#type' => 'file',
  114. '#title' => t('File'),
  115. '#size' => 40,
  116. '#default_value' => '',
  117. );
  118. if ($node->file->filepath) {
  119. $form['file']['#description'] .= t('A file already exists, if you upload another file the current file will be replaced.');
  120. }
  121. if ($node->nid) {
  122. // the validate function doesn't get a full copy of the node, only the
  123. // data posted from the form. use this field to tell the validator
  124. // the path of the node's current file.
  125. $form['current_file'] = array(
  126. '#type' => 'value',
  127. '#value' => $node->file->filepath
  128. );
  129. }
  130. return $form;
  131. }
  132. /**
  133. * Implementation of hook_validate().
  134. *
  135. * Verify that there is a file attached to the node.
  136. */
  137. function fileupload_validate(&$node) {
  138. // Check for a new file upload.
  139. if ($file = file_check_upload('file')) {
  140. $node->file = $file;
  141. }
  142. // Make sure there is an upload or an existing file.
  143. if (!$file && !file_exists($node->current_file)) {
  144. form_set_error('file', t('A file must be provided.'));
  145. }
  146. }
  147. /**
  148. * Implementation of hook_execute().
  149. *
  150. * If a file was uploaded save it before updating the database.
  151. */
  152. function fileupload_execute(&$node) {
  153. // if a file was uploaded, move it to the files directory
  154. if ($file = file_check_upload('file')) {
  155. $node->file = file_save_upload($file, file_directory_path(), false);
  156. }
  157. }
  158. /**
  159. * Implementation of hook_load().
  160. *
  161. * The node is being loaded. Load the file's information from the database so
  162. * it can be added to the node.
  163. */
  164. function fileupload_load($node) {
  165. $extras = new StdClass();
  166. $extras->file = db_fetch_object(db_query('SELECT filename, filepath, filemime, filesize FROM {fileupload} WHERE nid = %d', $node->nid));
  167. return $extras;
  168. }
  169. /**
  170. * Implementation of hook_insert().
  171. *
  172. * The node is being created, save the file's infomation to the database.
  173. */
  174. function fileupload_insert($node) {
  175. $file = $node->file;
  176. 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);
  177. }
  178. /**
  179. * Implementation of hook_update().
  180. *
  181. * The node is being updated, save changes to the file infomation to the database.
  182. */
  183. function fileupload_update($node) {
  184. $newfile = $node->file->filepath;
  185. $oldfile = db_result(db_query('SELECT filepath FROM {fileupload} WHERE nid = %d', $node->nid));
  186. // only make changes if there was a new file.
  187. if ($newfile) {
  188. if ($newfile != $oldfile) {
  189. // delete the old file
  190. file_delete($oldfile);
  191. }
  192. // update the database
  193. $file = $node->file;
  194. 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);
  195. }
  196. }
  197. /**
  198. * Implementation of hook_delete().
  199. *
  200. * The node is being delete, remove the file and any information.
  201. */
  202. function fileupload_delete($node) {
  203. file_delete($node->filepath);
  204. db_query("DELETE FROM {fileupload} WHERE nid = %d", $node->nid);
  205. }
Login or register to post comments