drupal form 中图片上传

1.创建url 链接到form

$items['qianfeng/add'] = array(
    'title' => t('添加信息'),
    '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,
  );

创建form元素

$barcode_image 为图片路径

function qianfeng_add($form, $form_state,$barcode_id){

if(!empty($barcode_image)){
 $form['barcode_image_current'] = array(
   '#title' => t('图片'),
   '#prefix' => l('<img class="barcode_image" src='.$barcode_image.'>',$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('提交'),
      '#type' => 'submit',
      '#value'=>'提交',
      '#weight' =>5
  );
  $form['#attributes']['enctype'] = 'multipart/form-data';
  return $form;
}

form 提交函数

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("图片格式不正确,正确的格式(gif,jpg,png,bmp)!",'warning');
    return false;
  }
  if(intval($file['size'])>2097152){
   drupal_set_message("图片大小超出2M,请上传2M以下的图片!",'warning');
   return false;
  }
$id = move_uploaded_file($file['tmp_name'],$picName);
$fields['image_name'] = $name.'.'.$ftype[1];
     $fields['image_path'] = "sites/default/files/barcode/";

}
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值