Showing posts with label upload file in drupal. Show all posts
Showing posts with label upload file in drupal. Show all posts

Saturday, January 29, 2011

Uploading Files in Drupal

  1. function invoice_perm()
  2. {
  3. return array('access content invoice');
  4. }

  5. function invoice_menu(
  6. {
  7. $items['invoice'] = array(
  8. 'title' => 'Invoice Entry',
  9. 'page callback' => 'invoice_editpage',
  10. 'access callback' =>'user_access',
  11. 'access arguments' => array('access content invoice'),
  12. 'type' => MENU_NORMAL_ITEM,
  13. );
  14. return $items;
  15. }

  16. function invoice_editpage()
  17. {
  18. $output = t('Invoice Entry Form.');

  19. // Return the HTML generated from the $form data structure.
  20. $output .= drupal_get_form('invoice_nameform');
  21. return $output;
  22. }

  23. function invoice_nameform($form_id, $form_state = NULL)
  24. {

  25. //File Refernce name

  26. $form['invoice_title'] = array(
  27. '#type' => 'textfield',
  28. '#title'=>t('Invoice Title'),
  29. '#size'=>20,
  30. );

  31. $form['#attributes'] = array('enctype' => "multipart/form-data");
  32. $form['invoicefileattachment'] = array(
  33. '#type' => 'file',
  34. '#title' => t('Browse the Scan copy of the invoice'),
  35. '#required' => FALSE,
  36. '#size'=>20,
  37. '#default_value' => $form_state['values']['invoicefileattachment'],
  38. );
  39. $form['submit1'] = array(
  40. '#type' => 'submit',
  41. '#value' => t('Register'),
  42. );
  43. return $form;
  44. }

  45. function invoice_nameform_submit($form_id, $form_state)
  46. {

  47. global $user;
  48. profile_load_profile($user);
  49. $uid=$user->uid;
  50. $empid=$user->employee_code;
  51. $userip = $_SERVER['REMOTE_ADDR'];
  52. $dt=date('Y-m-d H:i:s');
  53. $state_code=$user->state_code;

  54. if($state_code==30)
  55. $abc='TR';
  56. else
  57. $abc='TN';

  58. $limits = array () ;
  59. $invoice_title = $form_state['values']['invoice_title'];
  60. $po_title=$form_state['values']['po_title'];
  61. $path123=file_directory_path();


  62. $file = file_save_upload('invoicefileattachment', $validators, file_directory_path());

  63. $filename = $file->filename;
  64. $src = file_directory_path() . '/' . $filename;
  65. $dest = $abc.'/'.$filename;
  66. $path_file=$path123.'/'.$abc.'/'.$filename;

  67. if(isset($filename))
  68. {
  69. file_copy($src, $dest, FILE_EXISTS_REPLACE);
  70. file_delete($path123.'/'.$filename);
  71. drupal_set_message (" Invoice File saved", "status");
  72. file_set_status ($file, FILE_STATUS_PERMANENT);
  73. db_query("INSERT into drupal_txn_file_upload(uid ,
  74. filename,
  75. filepath,
  76. filesize,
  77. state_code,
  78. category,
  79. timestamp,title)
  80. VALUES('%d','%s','%s','%d','%d','%s','%d','%s')",$uid,$filename,$path_file,$file_size,$state_code,$category,time(),$invoice_title);
  81. }