Upload file trong drupal như thế nào?

public
group: Vietnamese
phuong.phuong - Mon, 2008-03-31 09:29

Mình viết 1 module node có field type là "file" để upload 1 image,
làm thế nào để check kích thước file, format, và lưu file vào thư mục "files"
Các bạn chỉ giúp newbie với.

Bản vui lòng gửi mã

thehong@drupal.org - Mon, 2008-03-31 09:36

Bản vui lòng gửi mã nguồn đang phát triển, TH sẽ góp ý chỉnh sửa.

Thế Hồng

Mã nguồn đây

phuong.phuong - Mon, 2008-03-31 10:23

<?php
// $Id: filename promotion.module$

/**
* @file
* Provides the Promotion node type
*/

/**
* Implementation of hook_node_info()
*
* @return array, each item of array is a node type
*/
function promotion_node_info() {
return array(
'promotion' => array(
'name' => t('Promotion'),
'module' => 'promotion',
'description' => t('Promotion module'),
'has_title' => TRUE,
'title_label' => t('Promotion Name'),
'has_body' => TRUE,
'body_label' => t('Promotion Description'),
'locked' => TRUE
)
);
}

/**
* Implementation of hook_menu().
*/
function promotion_menu($may_cache) {
$items = array();
// Do not cache this menu item during the development of this module.
if (!$may_cache) {
$items[] = array(
'path' => 'node/add/promotion',
'title' => t('Promotion'),
'access' => user_access('create promotion')
);
}
return $items;
}

/**
* Implementation of hook_access().
*/
function promotion_access($op, $node) {
global $user;

// Only users with permission to do so may create this node type.
if ($op == 'create') {
    return user_access('create promotion');
}

// Users who create a node may edit or delete it later
if ($op == 'update' || $op == 'delete') {
    if (user_access('edit own promotion') &amp;&amp; ($user-&gt;uid == $node-&gt;uid)) {
        return TRUE;
    }
}

}

/**
* Implementation of hook_perm().
*/
function promotion_perm() {
return array('create promotion', 'edit own promotion');
}

/**
* Implementation of hook_form().
*/
function promotion_form(&$node) {
$type = node_get_types('type', $node);

$form['#attributes']['enctype'] = 'multipart/form-data';

$form['title'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; check_plain($type-&gt;title_label),
    '#required' =&gt; TRUE,
    '#default_value' =&gt; $node-&gt;title,
    '#weight' =&gt; -5
);

$form['body'] = array(
    '#type' =&gt; 'textarea',
    '#title' =&gt; check_plain($type-&gt;body_label),
    '#default_value' =&gt; $node-&gt;body,
    '#required' =&gt; TRUE
);

$form['brands'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Brands'),
    '#default_value' =&gt; $node-&gt;brands,
    '#description' =&gt; t('A comma-separated list of terms describing this content. Example: funny, bungee jumping, "Company, Inc.".'),
    '#weight' =&gt; -6
);

$form['code'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Promotion Code'),
    '#default_value' =&gt; $node-&gt;code,
    '#required' =&gt; TRUE
);

$form['barcode'] = array(
    '#type' =&gt; 'file',
    '#title' =&gt; t('Upload barcode'),
    '#required' =&gt; FALSE,
    '#description' =&gt; 'Should be upload JPEG, GIF, BMP with image size (w: 120, h: 120)'
    );

    return $form;

}

/**
* Implementation of hook_validate().
*/
function promotion_validate(&$node) {

}

/**
* Implementation of hook_insert()
*/
function promotion_insert($node) {

}

Ở hook_insert (), bạn

thehong@drupal.org - Mon, 2008-03-31 10:42

Ở hook_insert (), bạn thử thêm vào dòng sau:

<?php
$file
= file_check_upload ('barcode');
drupal_set_message (serialize ($file));
?>

Muốn lưu tập tin thì thêm vào dòng:

<?php
  $file
= file_save_upload ($file, 'somewhere');
?>

PS: Bỏ source code vào giữa thẻ đóng/mở PHP code để hệ thống tô màu, dễ quan sát hơn.

Thế Hồng

Cũng có thể sử dụng

thehong@drupal.org - Mon, 2008-03-31 10:48

Cũng có thể sử dụng http://drupalbin.com/ để dán code, rồi cho link.

Thế Hồng

Lỗi gì đây

phuong.phuong - Mon, 2008-03-31 11:15

Mình viết như vầy nhưng sao nó lỗi ko upload lên được

<?php
function promotion_insert($node) {
   
$dir = base_path() . 'files';
   
$file = file_check_upload('barcode');
   
//drupal_set_message (serialize($file));
   
$file = file_save_upload($file, $dir);
}
?>

Thử đoạn này xem

thehong@drupal.org - Mon, 2008-03-31 14:29

Có thể thư mục files ở server của bạn không cho Drupal ghi, chmod nó thành 666. Bạn có thể gửi thông báo lỗi lên? Thử đoạn này xem sao:

<?php
function promotion_insert($node) {
 
$files_dir = file_directory_path (); // Determine the default 'files' directory.
 
if (file_check_directory($files_dir, 1)) {
   
$file = file_check_upload ('barcode');
   
$file = file_save_upload ($file, $files_dir);
    if (
$file) {
     
// ...
   
}
    else {
     
// ...
   
}
  }
  else {
   
drupal_set_message (t ('@files_dir is not writable.', array ('@files_dir' => $files_dir)), 'error');
  }
}
?>

Thế Hồng

Chạy rùi

phuong.phuong - Tue, 2008-04-01 02:53

Thử đoạn Thế Hồng viết thì chạy ngon rùi :)

Nhưng làm phiền tiếp đây:

1/ Làm sao kiểm tra được kích thước file ảnh, ví dụ max 90x90 thôi và giới hạn format file (jpg, gif, png)

2/ Khi mình để required = TRUE như code dưới, thì mặc dù lúc submit đã browser chọn 1 file ảnh rồi nhưng vẫn báo là field đó cần required hix hix

<?php
$form
['barcode'] => array(
//...
'#type' => 'file',
'#required' => TRUE
//...
);
?>

Dòng này giúp ích

thehong@drupal.org - Tue, 2008-04-01 03:54

Dòng này giúp ích đây:

<?php
  drupal_set_message
(serialize ($file));
?>

Thế Hồng

Dòng đó in ra như vầy,

phuong.phuong - Tue, 2008-04-01 04:28

Dòng đó in ra như vầy, ko có thông tin về chiều dài rộng:

<?php
O
:8:"stdClass":5:{s:8:"filename";s:8:"logo.jpg";s:8:"filepath";s:14:"files/logo.jpg";s:8:"filemime";s:10:"image/jpeg";s:8:"filesize";i:5399;s:6:"source";s:4:"logo";}
?>

Mình hỏi thêm là khi mình tạo Vocabulary và Term (tạo bằng giao diện admin) liên kết với node type Promotion thì trên form add promotion tự động show ra mấy cái Category ra luôn, làm sao cho nó ko tự show mà mình tự viết form cho các field category (dạng list-box) đó?

Tham khảo hàm:

thehong@drupal.org - Sat, 2008-04-26 04:28

Tham khảo hàm: user_validate_picture ()
Tôi cũng có viết một module thí dụ Kiểm tra hình ảnh tải lên

Thế Hồng

Thanks Thế Hồng

phuong.phuong - Tue, 2008-04-01 05:43

Thanks Thế Hồng.

Làm sao kiểm tra được kích thước file ảnh, ví dụ max 90x90 thôi và giới hạn format file (jpg, gif, png)

--> image_get_info() (http://api.drupal.org/api/function/image_get_info/5)


Mình hỏi thêm là khi mình tạo Vocabulary và Term (tạo bằng giao diện admin) liên kết với node type Promotion thì trên form add promotion tự động show ra mấy cái Category ra luôn, làm sao cho nó ko tự show mà mình tự viết form cho các field category (dạng list-box) đó?

->> Còn cái này ai có ý kiến gì ko?

Lạc chủ đề rồi

thehong@drupal.org - Tue, 2008-04-01 05:57

Lạc chủ đề rồi, bạn tạo topic mới, cũng nhớ đánh thẻ, để sau này người khác dễ tìm hơn.

Thế Hồng

Sorry, quay lại chủ đề upload file

phuong.phuong - Tue, 2008-04-01 07:33

Lỗi mới phát hiện:

//xem lại code của Promition node type phía trên
//nếu in $node ra thì ko nhận được giá trị của element có #type = 'file'

<?php
function promotion_insert($node) {
print_r($node);
}
?>

//Output
stdClass Object
(
...
[barcode] => //ko có gì cả
...
)

Tại sao ở trên đã

thehong@drupal.org - Tue, 2008-04-01 23:58

Element có #type file chỉ có thể truy cập theo cách trên (http://groups.drupal.org/node/10305#comment-33193), không như các element khác.

Thế Hồng

OK rồi

phuong.phuong - Wed, 2008-04-02 04:53

Mình làm ok rùi, thanks TH.