PHP使用CURL下傳檔案

來源:互聯網
上載者:User
PHP使用CURL上傳檔案
用curl上傳檔案的話很方便,什麼header,post串都不用產生了,用fsockopen要寫一堆
curl:
$file = array("upimg"=>"@E:/png.png");//檔案路徑,前面要加@,表明是檔案上傳.$curl = curl_init("http://localhost/a.php");curl_setopt($curl,CURLOPT_POST,true);curl_setopt($curl,CURLOPT_POSTFIELDS,$file);curl_exec($curl);

fsockopen
$uploadFile = file_get_contents("E:/png.png");$boundary   = md5(time());$postStr .="--".$boundary."\r\n";//邊界開始,注意預設比header定義的boundary多兩個'-'$postStr .="Content-Disposition: form-data; name=\"upimg\"; filename=\"E:/png.png\"\r\n";$postStr .="Content-Type: image/png\r\n\r\n";$postStr .=$uploadFile."\r\n";$postStr .="--".$boundary."\r\n";//邊界結束fwrite($fp,"POST /a.php HTTP/1.0\r\n");fwrite($fp,"Content-Type: multipart/form-data; boundary=".$boundary."\r\n");fwrite($fp,"Content-length:".strlen($postStr)."\r\n\r\n");fwrite($fp,$postStr);while (!feof($fp)){     echo fgets($fp, 128);}fclose($fp);print_r($_FILES);

1 樓 pz9042 2011-10-24

我想問下,curl可以上傳檔案嗎,依 你的代碼,在指定url列印$_FILES,確實有資訊,但,我在所謂的臨時檔案夾雷根本沒找到檔案,更無法上傳檔案,樓主,希望你貼出主要的代碼,你現在的代碼,只不過是填寫了資訊,根本沒有上傳的代碼

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.