drupal form 中圖片上傳

來源:互聯網
上載者:User

標籤:

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/";

}
}



drupal form 中圖片上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.