Ví dụ về tệp bài đăng php curl multipart/form-data
Qua 4 bài vừa rồi bạn thấy CURL khá hay phải không nào? Bài viết này đã được đăng tại freetuts. net , không được sao chép dưới mọi hình thức. To upload file in php chúng ta thực hiện ba thao tác
To Unknown than you xem sơ đồ dưới đây Nhìn vào sơ đồ bạn thấy hơi khác so với thông thường không phải thông thường nào, thông thường thì chúng ta chỉ cần 2 file thôi đó là file Bài viết này đã được đăng tại [free tuts. bọc lưới] 1. mẫu hồ sơ. php
2. tập tin cuộn tròn. php// Nếu submit form if (isset($_POST['btnUpload'])) { // Lấy thông tin file upload $filename = $_FILES['file']['name']; $filedata = $_FILES['file']['tmp_name']; $filesize = $_FILES['file']['size']; // Nếu file OK if ($filedata != '') { $headers = array("Content-Type:multipart/form-data"); // Đối với filedata phải có ký hiệu @ ở trước $postfields = array("filedata" => "@$filedata", "filename" => $filename); // Khởi tạo CURL // URL trỏ đến file upload.php $ch = curl_init('http://localhost/2_develop/tour/upload.php'); // Cấu hình có sử dụng header // Vì chúng ta đang gửi file nên header của nó // phải ở dạng Content-Type:multipart/form-data curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // Cấu hình sử dụng method POST curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); // Thiết lập có gửi file và thông tin file curl_setopt($ch, CURLOPT_INFILESIZE, $filesize); // Cấu hình return curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Thực thi curl_exec($ch); // Nếu không tồn tại lỗi nào trong CURL if(!curl_errno($ch)) { $info = curl_getinfo($ch); if ($info['http_code'] == 200){ echo 'Upload thành công'; } } else { echo curl_error($ch); } // Đóng CURL curl_close($ch); } else { echo 'Bạn chưa chọn file để upload'; } } 3. tải lên tệp. php// Đường dẫn upload $uploadpath = "upload/"; // Nhận thông tin $filedata = $_FILES['filedata']['tmp_name']; $filename = $_POST['filename']; if ($filedata != '' && $filename != ''){ // Dùng hàm copy để lưu vào thay vì hàm move_upload_file như thông thường copy($filedata,$uploadpath.$filename); }Trong file này các bạn thấy bất thường là chúng ta sử dụng hàm // Nếu submit form if (isset($_POST['btnUpload'])) { // Lấy thông tin file upload $filename = $_FILES['file']['name']; $filedata = $_FILES['file']['tmp_name']; $filesize = $_FILES['file']['size']; // Nếu file OK if ($filedata != '') { $headers = array("Content-Type:multipart/form-data"); // Đối với filedata phải có ký hiệu @ ở trước $postfields = array("filedata" => "@$filedata", "filename" => $filename); // Khởi tạo CURL // URL trỏ đến file upload.php $ch = curl_init('http://localhost/2_develop/tour/upload.php'); // Cấu hình có sử dụng header // Vì chúng ta đang gửi file nên header của nó // phải ở dạng Content-Type:multipart/form-data curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // Cấu hình sử dụng method POST curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); // Thiết lập có gửi file và thông tin file curl_setopt($ch, CURLOPT_INFILESIZE, $filesize); // Cấu hình return curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Thực thi curl_exec($ch); // Nếu không tồn tại lỗi nào trong CURL if(!curl_errno($ch)) { $info = curl_getinfo($ch); if ($info['http_code'] == 200){ echo 'Upload thành công'; } } else { echo curl_error($ch); } // Đóng CURL curl_close($ch); } else { echo 'Bạn chưa chọn file để upload'; } }0 chứ không phải hàm // Nếu submit form if (isset($_POST['btnUpload'])) { // Lấy thông tin file upload $filename = $_FILES['file']['name']; $filedata = $_FILES['file']['tmp_name']; $filesize = $_FILES['file']['size']; // Nếu file OK if ($filedata != '') { $headers = array("Content-Type:multipart/form-data"); // Đối với filedata phải có ký hiệu @ ở trước $postfields = array("filedata" => "@$filedata", "filename" => $filename); // Khởi tạo CURL // URL trỏ đến file upload.php $ch = curl_init('http://localhost/2_develop/tour/upload.php'); // Cấu hình có sử dụng header // Vì chúng ta đang gửi file nên header của nó // phải ở dạng Content-Type:multipart/form-data curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // Cấu hình sử dụng method POST curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); // Thiết lập có gửi file và thông tin file curl_setopt($ch, CURLOPT_INFILESIZE, $filesize); // Cấu hình return curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Thực thi curl_exec($ch); // Nếu không tồn tại lỗi nào trong CURL if(!curl_errno($ch)) { $info = curl_getinfo($ch); if ($info['http_code'] == 200){ echo 'Upload thành công'; } } else { echo curl_error($ch); } // Đóng CURL curl_close($ch); } else { echo 'Bạn chưa chọn file để upload'; } }1 nhé Lời kếtCác bạn chạy file |