• Downloading from our site will require you to have a paid membership. Upgrade to a Premium Membership from 10$ a month today!

    Dont forget read our Rules! Also anyone caught Sharing this content will be banned. By using this site you are agreeing to our rules so read them. Saying I did not know is simply not an excuse! You have been warned.

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
Thread starter Title Forum Replies Date
khanhsak3r Help Upload file lên host khác Thảo luận wap việt 6
Admin Share code dropbox uploader hỗ trợ upload file lên dropbox Mã nguồn web 1
Admin Share code ajaxplorer 4.2.3 - quản lý file, upload file lên host Mã nguồn web 0
N Hướng dẫn Upload File Lên Mạng Miễn Phí Nhanh Nhất Thảo luận wap việt 2
Admin Hướng dẫn upload file lên box.com & get link bằng operamini Thủ thuật ĐTDĐ 0
Yeukodamnoi Có cách nào có thể upload file lên host qua url không Thùng rác dọn dẹp thường xuyên 9
L Ai hướng dẫn sử dụng toot upload file lên xtgem nào!! Thảo luận chung 18
F Đỉnh chóp: Fshare Upload Triệu File Săn Thưởng 50 Triệu Đồng Hấp Dẫn ! Tin tức, sự kiện thường ngày 0
cuongpro9x Share Loại bỏ Tiếng Việt và khoảng trắng của File Upload trong WordPress Xenforo 1
T CodeIgniter 10- Thư viện upload file PHP 1
Myshare Share code zFiles v1.0.4 Code Web Upload Chia Sẻ File Mã nguồn web 6
DavidC Xin Clone/Code/Source Code Upload File Cho Web Hosting! All Shared Scripts 1
Admin Share code quản lý, upload dữ liệu file-gator.com Mã nguồn web 0
Admin Share code upload đóng đấu file manifest.mf trong java Mã nguồn wap 0
Admin Share code upload chia sẻ file giống dropbox.com Mã nguồn web 0
L trang upload file nặng miễn phí cho anh em Thảo luận chung 5
Admin Download - Upload File Interface PSD - giao diện upload phong cách Metro HTML & CSS 0
Admin Hướng dẫn fix lỗi upload failed due to failure writing temporary file Vbb tutorial 0
Myshare Share Bảo mật chống hack wap upload bằng file .htaccess Chuyên đề htaccess 0
boy94 Share code web/wap upload file All Shared Scripts 6
Admin Share tool auto upload xtgem mod đăng nhập tự động unzip file Wap builder, wapego, xtgem, wen.ru, wapka, wap4 6
Admin Share tool auto upload xtgem 1.5 mod đăng nhập, up file + unzip Wap builder, wapego, xtgem, wen.ru, wapka, wap4 8
Admin Share mod upload multi file name cho johncms Johncms 0
Admin Mediafire chính thức phát hành tools upload, share file ngay trên desktop Phần mềm 0
Thichquangboom Fshare Upload Award T01.2022: Upload mạnh tay - Rinh ngay giải đặc biệt 10 Triệu Đồng Tin tức, sự kiện thường ngày 0
Thichquangboom Fshare Upload Award Bùng Nổ Tháng Đặc Biệt – Tổng Giải Thưởng Lên Đến 50 Triệu Đồng Tin tức, sự kiện thường ngày 0
Thichquangboom Fshare Upload Award T11 | Upload rất vui – Nhận quà hấp dẫn đến 20 triệu đồng Tin tức, sự kiện thường ngày 0
Thichquangboom Fshare Upload Award T10 | Upload rất vui – Nhận quà hấp dẫn đến 20 triệu đồng Tin tức, sự kiện thường ngày 0
Thichquangboom FSHARE UPLOAD AWARD THÁNG 9 | UPLOAD LIỀN TAY – SAY MÊ NHẬN GIẢI Tin tức, sự kiện thường ngày 0
Thichquangboom UPLOAD MẠNH TAY – RINH NGAY QUÀ KHỦNG – HOT NHẤT T8.2021 Tin tức, sự kiện thường ngày 1
Thichquangboom UPLOAD MẠNH TAY – HĂNG SAY NHẬN GIẢI – ĐUA THÁNG 7.2021 Tin tức CNTT 0
Thichquangboom UPLOAD MẠNH TAY – HĂNG SAY NHẬN GIẢI – ĐUA QUÝ 2.2021 Tin tức CNTT 0
Thichquangboom Khởi động cuộc hành trình mới tìm kiếm Fshare Upload Award tháng 05/2021 Tin tức CNTT 0
Thichquangboom Khởi động cuộc hành trình mới tìm kiếm Fshare Upload Award tháng 04 Thông tin khuyến mại Fshare 0
Thichquangboom Fshare ra mắt tính năng Thống kê Upload giúp người dùng quản lý tập tin hiệu quả Tin tức CNTT 0
Thichquangboom Upload Guest – Giải pháp chia sẻ nội dung nhanh chóng, an toàn không cần đăng nhập Tin tức CNTT 0
F Cuộc thi UPLOAD QUÝ 1 – Cao thủ uploader tranh tài giải thưởng 15.000.000VNĐ Tin tức, sự kiện thường ngày 0
Admin A2soft-pics - (A2)Upload Postimages picture xenforo 2 Xenforo 0
Thichquangboom FSHARE RA MẮT TÍNH NĂNG UPLOAD GUEST – CHIA SẺ NỘI DUNG KHÔNG CẦN ĐĂNG NHẬP Tin tức CNTT 0
Admin Yilmaz - Image upload mod for vB5 Add-ons 0
Thichquangboom FSHARE UPLOAD AWARDS – EVENT UPLOADER THÁNG 12 Tin tức CNTT 0
Admin Share code wap upload mod full Mã nguồn wap 3
Admin Upload Avatar From URL xenforo 2 Xenforo 0
dammechiase Hướng Dẫn Chi Tiết Cách Kiếm Tiền Từ Trang Upload Kiếm Tiền Hấp Dẫn Vipshare 2.5$/1000 lượt dowload Trò chơi 0
Npnken Share LickLink ra mắt hệ thống upload kiếm tiền tại VipShare Tut, tool, mmo 0
G Share Tạo trang upload tập tin với PHP PHP 2
Thichquangboom Fshare.vn ra mắt tính năng upload không cần tài khoản Thông tin khuyến mại Fshare 0
Thichquangboom Tính năng mới: Follow folder - gia tăng lợi ích cho người upload fshare.vn Thông tin khuyến mại Fshare 0
G Hỏi [vbb] upload ảnh avatar qua url bị báo Access denied. Vbulletin 0
K Thảo luận Mua wap upload wapka Wap builder, wapego, xtgem, wen.ru, wapka, wap4 0

Similar threads

New posts New threads New resources

Back
Top