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,027

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



 

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

 



Topic : 100506



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



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




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

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


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

ขอบคุณครับ



Tag : PHP, CakePHP







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

 

No. 1



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

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

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

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







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


 

No. 2



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

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

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

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


Code (PHP)
<?php
$uploaddir = realpath('./') . '/';
$uploadfile = $uploaddir . basename($_FILES['file_contents']['name']);
echo '<pre>';
	if (move_uploaded_file($_FILES['file_contents']['tmp_name'], $uploadfile)) {
	    echo "File is valid, and was successfully uploaded.\n";
	} else {
	    echo "Possible file upload attack!\n";
	}
	echo 'Here is some more debugging info:';
	print_r($_FILES);
	echo "\n<hr />\n";
	print_r($_POST);
print "</pr" . "e>\n";
?>


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,719 )
บทความ ( 8 )



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


Code (PHP)
<?php

header('Content-type: application/json');

$output = [];


if (empty($_FILES['image']['name']) || !isset($_FILES['image']['error'])) {
    $_FILES['image']['error'] = 4;
}

if ($_POST && $_FILES) {
    // @link https://www.php.net/manual/en/features.file-upload.errors.php#115746
    $phpFileUploadErrors = array(
        0 => 'There is no error, the file uploaded with success',
        1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
        2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
        3 => 'The uploaded file was only partially uploaded',
        4 => 'No file was uploaded',
        6 => 'Missing a temporary folder',
        7 => 'Failed to write file to disk.',
        8 => 'A PHP extension stopped the file upload.',
    );
    $uploadFolder = __DIR__ . DIRECTORY_SEPARATOR . 'uploaded';
    $allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];

    if (isset($_FILES['image']['error']) && $_FILES['image']['error'] !== UPLOAD_ERR_OK) {
        $output['error'] = true;
        $output['message'] = $phpFileUploadErrors[$_FILES['image']['error']];
    } else {
        if (!is_dir($uploadFolder)) {
            $oldumask = umask(0);
            mkdir($uploadFolder, 0777);
            umask($oldumask);
            unset($oldumask);
        }

        $moveToFileName = $uploadFolder . DIRECTORY_SEPARATOR . $_FILES['image']['name'];
        if (move_uploaded_file($_FILES['image']['tmp_name'], $moveToFileName)) {
            $Finfo = new finfo();
            $output['uploadedMimeType'] = $Finfo->file($moveToFileName, FILEINFO_MIME_TYPE);
            unset($Finfo);
            if (in_array(strtolower($output['uploadedMimeType']), $allowedMimeTypes)) {
                http_response_code(201);
                $output['uploadSuccess'] = true;
                $output['message'] = 'Uploaded completed.';
            } else {
                http_response_code(400);
                $output['uploadSuccess'] = false;
                $output['error'] = true;
                $output['message'] = 'You have uploaded disallowed file types.';

                @unlink($moveToFileName);
            }
        } else {
            $output['error'] = true;
            $output['message'] = 'Unable to move uploaded file.';
        }

        unset($moveToFileName);
    }

    unset($uploadFolder);
}



$output['debug']['_POST'] = $_POST;
$output['debug']['_FILES'] = $_FILES;

echo json_encode($output);

upload.php



Code (PHP)
<?php
$imageFile = __DIR__ . DIRECTORY_SEPARATOR . 'image.jpg';
$targetURL = (strtolower($_SERVER['REQUEST_SCHEME']) === 'https' ? 'https://' : 'http://')
    . ($_SERVER['HTTP_HOST'] ?? 'localhost')
    . (isset($_SERVER['REQUEST_URI']) ? dirname($_SERVER['REQUEST_URI']) : '')
    . '/upload.php';


if (!is_file($imageFile)) {
    http_response_code(404);
    echo '<p>Image file was not found. Please prepare image file at this location: <strong>' . $imageFile . '</strong></p>';
    exit();
}


$headers = [];
$allHeaders = [];
$postFields = [];


// create post fields
$postFields['hidden-input'] = 'hidden value (from cURL).';
// create upload file to post fields.
$Finfo = new finfo();
$fileMimeType = $Finfo->file($imageFile, FILEINFO_MIME_TYPE);
unset($Finfo);
$CurlFile = new CURLFile($imageFile, $fileMimeType, 'curl-upload-' . basename($imageFile));
$postFields['image'] = $CurlFile;
unset($CurlFile, $fileMimeType);


// start cURL.
$ch = curl_init($targetURL);
// don't echo out immediately.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// method POST.
curl_setopt($ch, CURLOPT_POST, true);
// disable SSL verify. this is for test with self signed (local host) only. remove this on production site.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
// response headers work.
curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'headerFunction');
// POST data with file upload.
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);

// execute cURL.
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if (curl_errno($ch)) {
    http_response_code(500);
    echo '<p>cURL error: ' . curl_error($ch) . '</p>';
    curl_close($ch);
    exit();
}

// close cURL.
curl_close($ch);
unset($ch, $postFields);


http_response_code($httpCode);
if (isset($headers['content-type'])) {
    header('Content-type: ' . $headers['content-type']);
}
unset($headers);


// add headers to `$response` to see what we have here.
$responseJson = json_decode($response);
unset($response);
$responseJson->debug->allHeaders = $allHeaders;
$responseJson->debug->httpCode = $httpCode;
// echo out cURL response.
echo json_encode($responseJson);
unset($responseJson);


/**
 * cURL header manipulatement function.
 *
 * @param resource $ch cURL resource
 * @param string $header A header line.
 * @return int Return length of header line.
 */
function headerFunction($ch, $header)
{
    global $allHeaders, $headers;
    if (!empty(trim($header))) {
        $headerExp = explode(':', $header, 2);
        if (count($headerExp) === 2) {
            $headers[strtolower(trim($headerExp[0]))] = trim($headerExp[1]);
        }
        $allHeaders[] = $header;
    }
    return (int) mb_strlen($header);
}// 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 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

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