1. Create a URL link to form
$ Items ['qianfeng/add'] = array (
'Title' => T ('add information '),
'Page callback' => 'drupal _ get_form ',
'Page arguments '=> array ('qianfeng _ add', 1 ),
'File' => 'videocloud _ qianfeng. pages. inc ',
'Access callback' => 'user _ is_logged_in ',
'Weight' =>-10,
'Type' => menu_callback,
);
Create form Element
$ Barcode_image indicates the image path.
Function qianfeng_add ($ form, $ form_state, $ barcode_id ){
If (! Empty ($ barcode_image )){
$ Form ['barcode _ image_current '] = array (
'# Title' => T ('image '),
'# Prefix' => L (' ', $ barcode_image, array ("html" => true )),
'# Weight' => 3,
);
}
$ Form ['barcode _ image'] = array (
'# Type' => 'file ',
'# Name' => 'barcode _ image ',
'# Required' => false,
'# Weight' => 4
);
$ Form ['submit '] = array (
'# Title' => T ('Submit '),
'# Type' => 'submit ',
'# Value' => 'Submit ',
'# Weight' => 5
);
$ Form ['# attributes'] ['enablesype '] = 'multipart/form-data ';
Return $ form;
}
Form submission Function
Function qianfeng_add_submit ($ form, $ form_state ){
$ TP = array ("GIF", "jpg", "PNG", "BMP ");
$ File = $ _ FILES ["barcode_image"];
If (! Empty ($ file ['name']) {
$ Path = "./sites/default/files/barcode /";
If (! File_exists ($ PATH )){
Mkdir ($ path, 0777, true );
}
$ Name = MD5 (Time (). Rand (1,10000 ));
$ Fname = $ file ['name'];
$ FTYPE = explode ('.', $ fname );
$ Picname = $ path. $ name. '.'. $ FTYPE [1];
If (! In_array ($ FTYPE [1], $ TP )){
Drupal_set_message ("incorrect image format, correct format (GIF, JPG, PNG, BMP )! ", 'Warning ');
Return false;
}
If (intval ($ file ['SIZE'])> 2097152 ){
Drupal_set_message ("the image size exceeds 2 MB. upload an image below 2 MB! ", 'Warning ');
Return false;
}
$ Id = move_uploaded_file ($ file ['tmp _ name'], $ picname );
$ Fields ['image _ name'] = $ name. '.'. $ FTYPE [1];
$ Fields ['image _ path'] = "sites/default/files/barcode /";
}
}