Image upload in Drupal form

Source: Internet
Author: User
Tags drupal

1. Create URL link to form

$items [' qianfeng/add '] = Array (
' title ' + t (' join 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,
);

Creating a FORM Element

$barcode _image as the picture path

function Qianfeng_add ($form, $form _state, $barcode _id) {

if (!empty ($barcode _image)) {
$form [' barcode_image_current '] = Array (
' #title ' and t (' Pictures '),
' #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 ' and T (' submit '),
' #type ' = ' Submit ',
' #value ' = ' Submit ',
' #weight ' =>5
);
$form [' #attributes '] [' enctype '] = ' 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 ("The picture is in the wrong format, the correct format (gif,jpg,png,bmp)!" ", ' warning ');
return false;
}
if (Intval ($file [' size ']) >2097152) {
Drupal_set_message ("Picture size exceeds 2M, please upload 2M below the picture!") ", ' warning ');
return false;
}
$id = Move_uploaded_file ($file [' Tmp_name '], $picName);
$fields [' image_name '] = $name. '. '. $ftype [1];
$fields [' image_path '] = "sites/default/files/barcode/";

}
}



Image upload in Drupal form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.