P

princenuce

New Member
cach mình đưa ra sau đây k phải là tối ưu nhất nhưng vì nó là một ứng dụng khá hay của cURL nên mình chia sẻ cho ai muốn biết. ý tưởng là mình có 1 ảnh nào đó hoạc 1 file nào đó ,mình có 2 host, 1 host chính để chạy code 1 host phụ để upload file lên, vậy mình sẽ thiết kế 2 script 1 để ở host chính nơi thực hiện các thao tác gửi file, 1 ở host phụ nơi thực hiện công việc nhận và lưu file.để đơn giản mình sẽ thiết kế 1 file là index.php đặt ở thư mục /bai1 trên teenclub.info, code như sau:
Code:
<meta charset="utf-8" />[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]<?[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]/* hàm get file */[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]function prince_get_file ($url) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  $p = curl_init ($url);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  curl_setopt ($p, CURLOPT_RETURNTRANSFER, 1);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  curl_setopt ($p, CURLOPT_BINARYTRANSFER, 1);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  $res = curl_exec ($p);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  curl_close ($p);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  return $res;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]
[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]/* hàm upload file */[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]function prince_upload_file ($url,$post) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  $p = curl_init ($url);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  curl_setopt ($p, CURLOPT_POST, 1);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  curl_setopt ($p, CURLOPT_POSTFIELDS, $post);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  curl_setopt ($p, CURLOPT_RETURNTRANSFER, 1);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  $res = curl_exec ($p);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  curl_close ($p);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  return $res;[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]  }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]$a = prince_get_file ('http://c.gocmobile.net/data/avatars/m/33/33383.jpg?1377001095'); /* lấy nguồn file dưới dạng binary */[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]$n = time () . '.jpg'; /* đặt tên file */[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]@mkdir ('up',0777); /* tạo thư mục upload  tạm thời*/[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]$im = fopen ('up/' . $n,'wb');[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]fwrite ($im, $a);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]fclose ($im); /* lưu file ảnh get được vào thư mục tạm thời trên host chính */[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]$file_name_with_full_path = realpath('./up/' . $n); /* lấy đường dẫn tuyệt đối */[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]$post = array('file_contents'=>'@'.$file_name_with_full_path); /* thiết kế mảng post dữ liệu */[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]echo prince_upload_file ('http://giaitri-truyenngan.rhcloud.com/upload.php',$post); /* upload và lấy kết quả trả về */[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]unlink ('up/' . $n); /* xóa file tạm*/[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]
và trên host phụ là http://giaitri-truyenngan.rhcloud.com/ mình tạo 1 file là upload.php trong thư mục gốc root như sau
Code:
<?php[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]$uploaddir = realpath('./') . '/';[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]$uploadfile = $uploaddir . basename($_FILES['file_contents']['name']);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        echo "đã được tải lên thành công.\n";[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        echo $uploaddir . basename($_FILES['file_contents']['name']);[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    } else {[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]        echo "thất bại!\n";[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]    }[/FONT][/COLOR]
[COLOR=#141414][FONT=Arial]?>
nhiệm vụ chỉ là nhận file gửi đến và lưu lên đĩa. vậy là ta có 1 code hoàn chỉnh r, hiện tại mình chỉ để 1 link ảnh cho việc test nên việc chế biến nó để nhận các link khác và kiểu file khác ngoài ảnh là quá đơn giản dành cho các bạn tự nghịch! như đã nói ở trên cách này không phải là tối ưu tuy nhiên nó là 1 phần trong curl mà lâu nay ít người chạm tới. hiện tại mình nghĩ dc 1 cách tooid ưu hn cách trên là vẫn 2 host A và B thì A nhận 1 link file -> gửi link cho B, B nhạn link và thực hiện tải file về lưu lại và trả lại đườn dẫn trược tiếp cho A, cách này cũng chỉ để dùng curl trong việc tải file, còn nếu như upload lên host chính r host chính gửi sang host phụ để lưu thì phải dùng cách 1 ở trên í. chúc vui
clear.png

demo live: http://teenclub.info/bai1/
 

Facebook Comments

Similar threads

Admin
Replies
1
Views
11K
x4love
X
Admin
Replies
0
Views
7K
AdminAdmin is verified member.
Admin
Admin
Replies
0
Views
2K
AdminAdmin is verified member.
Admin
cuongpro9x
Replies
1
Views
1K
AdminAdmin is verified member.
Admin
T
Replies
1
Views
2K
lamlun17
lamlun17
Back
Top