Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,038

HOME > PHP > PHP Forum > PHP จะใช้ cURL Upload File อัพรูปในแบบฟอร์มอันนี้ต้องเขียนยังไงอะครับ


[PHP] PHP จะใช้ cURL Upload File อัพรูปในแบบฟอร์มอันนี้ต้องเขียนยังไงอะครับ

 
Topic : 100506



โพสกระทู้ ( 377 )
บทความ ( 0 )



สถานะออฟไลน์



ผมได้แบบฟอร์มมาแบบนี้อะครับ

Code (PHP)
01.<form method="post" action="https://upload.facebook.com/_mupload_/composer/?site_category=m_basic" enctype="multipart/form-data">
02.    <input type="hidden" name="fb_dtsg" value="AQAagCQK" autocomplete="off">
03.    <input type="hidden" name="charset_test" value="€,´,€,´,水,Д,Є">
04.    <input type="hidden" name="return_uri_error" value="https://m.facebook.com/upload.php?target=XXXXX">
05.    <input type="hidden" name="return_uri" value="https://m.facebook.com/inwGame?v=timeline&;amp;fc=0">
06.    <input type="hidden" name="id" value="1123644243">
07.    <input type="hidden" name="target" value="304879169608441">
08.    <input type="hidden" name="ref" value="m_upload_pic">
09.    <input type="file" name="file1" autofocus="1"><br>
10.    <input value="อัพโหลด" type="submit" class="btn btnC"><br>
11.    <span class="mfss">คำบรรยายภาพ:</span><br>
12.    <textarea class="input" rows="2" name="caption"></textarea>
13.</form>


ไม่เคยทำให้ curl มันอัพรูปซักที แนะนำด้วยครับ

ขอบคุณครับ



Tag : PHP, CakePHP

Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2013-09-16 00:51:53 By : sakang View : 3574 Reply : 3
 

 

No. 1



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Code (PHP)
1.$file_name_with_full_path = realpath('./sample.jpeg');
2.$post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);
3.$ch = curl_init();
4.curl_setopt($ch, CURLOPT_URL,$target_url);
5.curl_setopt($ch, CURLOPT_POST,1);
6.curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
7.$result=curl_exec ($ch);
8.curl_close ($ch);

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-16 10:34:40 By : mr.win
 

 

No. 2



โพสกระทู้ ( 74,059 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Code (PHP)
01.<?php
02.    $target_url = 'http://127.0.0.1/accept.php';
03.        //This needs to be the full path to the file you want to send.
04.    $file_name_with_full_path = realpath('./sample.jpeg');
05.        /* curl will accept an array here too.
06.         * Many examples I found showed a url-encoded string instead.
07.         * Take note that the 'key' in the array will be the key that shows up in the
08.         * $_FILES array of the accept script. and the at sign '@' is required before the
09.         * file name.
10.         */
11.    $post = array('extra_info' => '123456','file_contents'=>'@'.$file_name_with_full_path);
12.  
13.        $ch = curl_init();
14.    curl_setopt($ch, CURLOPT_URL,$target_url);
15.    curl_setopt($ch, CURLOPT_POST,1);
16.    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
17.    $result=curl_exec ($ch);
18.    curl_close ($ch);
19.    echo $result;
20.?>


Code (PHP)
01.<?php
02.$uploaddir = realpath('./') . '/';
03.$uploadfile = $uploaddir . basename($_FILES['file_contents']['name']);
04.echo '<pre>';
05.    if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
06.        echo "File is valid, and was successfully uploaded.\n";
07.    } else {
08.        echo "Possible file upload attack!\n";
09.    }
10.    echo 'Here is some more debugging info:';
11.    print_r($_FILES);
12.    echo "\n<hr />\n";
13.    print_r($_POST);
14.print "</pr" . "e>\n";
15.?>


http://blog.derakkilgo.com/2009/06/07/send-a-file-via-post-with-curl-and-php/

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2013-09-16 10:35:47 By : mr.win
 

 

No. 3



โพสกระทู้ ( 4,765 )
บทความ ( 8 )



สถานะออฟไลน์


Code (PHP)
01.<?php
02. 
03.header('Content-type: application/json');
04. 
05.$output = [];
06. 
07. 
08.if (empty($_FILES['image']['name']) || !isset($_FILES['image']['error'])) {
09.    $_FILES['image']['error'] = 4;
10.}
11. 
12.if ($_POST && $_FILES) {
14.    $phpFileUploadErrors = array(
15.        0 => 'There is no error, the file uploaded with success',
16.        1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
17.        2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
18.        3 => 'The uploaded file was only partially uploaded',
19.        4 => 'No file was uploaded',
20.        6 => 'Missing a temporary folder',
21.        7 => 'Failed to write file to disk.',
22.        8 => 'A PHP extension stopped the file upload.',
23.    );
24.    $uploadFolder = __DIR__ . DIRECTORY_SEPARATOR . 'uploaded';
25.    $allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];
26. 
27.    if (isset($_FILES['image']['error']) && $_FILES['image']['error'] !== UPLOAD_ERR_OK) {
28.        $output['error'] = true;
29.        $output['message'] = $phpFileUploadErrors[$_FILES['image']['error']];
30.    } else {
31.        if (!is_dir($uploadFolder)) {
32.            $oldumask = umask(0);
33.            mkdir($uploadFolder, 0777);
34.            umask($oldumask);
35.            unset($oldumask);
36.        }
37. 
38.        $moveToFileName = $uploadFolder . DIRECTORY_SEPARATOR . $_FILES['image']['name'];
39.        if (move_uploaded_file($_FILES['image']['tmp_name'], $moveToFileName)) {
40.            $Finfo = new finfo();
41.            $output['uploadedMimeType'] = $Finfo->file($moveToFileName, FILEINFO_MIME_TYPE);
42.            unset($Finfo);
43.            if (in_array(strtolower($output['uploadedMimeType']), $allowedMimeTypes)) {
44.                http_response_code(201);
45.                $output['uploadSuccess'] = true;
46.                $output['message'] = 'Uploaded completed.';
47.            } else {
48.                http_response_code(400);
49.                $output['uploadSuccess'] = false;
50.                $output['error'] = true;
51.                $output['message'] = 'You have uploaded disallowed file types.';
52. 
53.                @unlink($moveToFileName);
54.            }
55.        } else {
56.            $output['error'] = true;
57.            $output['message'] = 'Unable to move uploaded file.';
58.        }
59. 
60.        unset($moveToFileName);
61.    }
62. 
63.    unset($uploadFolder);
64.}
65. 
66. 
67. 
68.$output['debug']['_POST'] = $_POST;
69.$output['debug']['_FILES'] = $_FILES;
70. 
71.echo json_encode($output);

upload.php



Code (PHP)
01.<?php
02.$imageFile = __DIR__ . DIRECTORY_SEPARATOR . 'image.jpg';
03.$targetURL = (strtolower($_SERVER['REQUEST_SCHEME']) === 'https' ? 'https://' : 'http://')
04.    . ($_SERVER['HTTP_HOST'] ?? 'localhost')
05.    . (isset($_SERVER['REQUEST_URI']) ? dirname($_SERVER['REQUEST_URI']) : '')
06.    . '/upload.php';
07. 
08. 
09.if (!is_file($imageFile)) {
10.    http_response_code(404);
11.    echo '<p>Image file was not found. Please prepare image file at this location: <strong>' . $imageFile . '</strong></p>';
12.    exit();
13.}
14. 
15. 
16.$headers = [];
17.$allHeaders = [];
18.$postFields = [];
19. 
20. 
21.// create post fields
22.$postFields['hidden-input'] = 'hidden value (from cURL).';
23.// create upload file to post fields.
24.$Finfo = new finfo();
25.$fileMimeType = $Finfo->file($imageFile, FILEINFO_MIME_TYPE);
26.unset($Finfo);
27.$CurlFile = new CURLFile($imageFile, $fileMimeType, 'curl-upload-' . basename($imageFile));
28.$postFields['image'] = $CurlFile;
29.unset($CurlFile, $fileMimeType);
30. 
31. 
32.// start cURL.
33.$ch = curl_init($targetURL);
34.// don't echo out immediately.
35.curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
36.// method POST.
37.curl_setopt($ch, CURLOPT_POST, true);
38.// disable SSL verify. this is for test with self signed (local host) only. remove this on production site.
39.curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
40.curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
41.// response headers work.
42.curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'headerFunction');
43.// POST data with file upload.
44.curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
45. 
46.// execute cURL.
47.$response = curl_exec($ch);
48.$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
49. 
50.if (curl_errno($ch)) {
51.    http_response_code(500);
52.    echo '<p>cURL error: ' . curl_error($ch) . '</p>';
53.    curl_close($ch);
54.    exit();
55.}
56. 
57.// close cURL.
58.curl_close($ch);
59.unset($ch, $postFields);
60. 
61. 
62.http_response_code($httpCode);
63.if (isset($headers['content-type'])) {
64.    header('Content-type: ' . $headers['content-type']);
65.}
66.unset($headers);
67. 
68. 
69.// add headers to `$response` to see what we have here.
70.$responseJson = json_decode($response);
71.unset($response);
72.$responseJson->debug->allHeaders = $allHeaders;
73.$responseJson->debug->httpCode = $httpCode;
74.// echo out cURL response.
75.echo json_encode($responseJson);
76.unset($responseJson);
77. 
78. 
79./**
80. * cURL header manipulatement function.
81. *
82. * @param resource $ch cURL resource
83. * @param string $header A header line.
84. * @return int Return length of header line.
85. */
86.function headerFunction($ch, $header)
87.{
88.    global $allHeaders, $headers;
89.    if (!empty(trim($header))) {
90.        $headerExp = explode(':', $header, 2);
91.        if (count($headerExp) === 2) {
92.            $headers[strtolower(trim($headerExp[0]))] = trim($headerExp[1]);
93.        }
94.        $allHeaders[] = $header;
95.    }
96.    return (int) mb_strlen($header);
97.}// headerFunction

curl-upload.php



https://rundiz.com/?p=805



สิ่งที่แตกต่างคือ โค้ดนี้ใช้คลาส CURLFile() ซึ่งเป็นของที่มีตั้งแต่ PHP 5.5+ และโค้ดใหม่ๆจะแนะนำให้ใช้อันนี้แทน.


ประวัติการแก้ไข
2022-06-12 00:37:58
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2022-06-12 00:37:07 By : mr.v
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : PHP จะใช้ cURL Upload File อัพรูปในแบบฟอร์มอันนี้ต้องเขียนยังไงอะครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2025 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่