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ã
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
<?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;
}
/**
* 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);
}
/**
* Implementation of hook_validate().
*/
function promotion_validate(&$node) {
}
/**
* Implementation of hook_insert()
*/
function promotion_insert($node) {
}
Ở hook_insert (), bạn
Ở 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
Cũng có thể sử dụng http://drupalbin.com/ để dán code, rồi cho link.
Thế Hồng
Lỗi gì đây
Mình viết như vầy nhưng sao nó lỗi ko upload lên được
<?phpfunction 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
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:
<?phpfunction 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
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
Dòng này giúp ích đây:
<?phpdrupal_set_message (serialize ($file));
?>
Thế Hồng
Dòng đó in ra như vầy,
Dòng đó in ra như vầy, ko có thông tin về chiều dài rộng:
<?phpO: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:
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
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
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
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'
<?phpfunction promotion_insert($node) {
print_r($node);
}
?>
//Output
stdClass Object
(
...
[barcode] => //ko có gì cả
...
)
Tại sao ở trên đã
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
Mình làm ok rùi, thanks TH.