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,確實有資訊,但,我在所謂的臨時檔案夾雷根本沒找到檔案,更無法上傳檔案,樓主,希望你貼出主要的代碼,你現在的代碼,只不過是填寫了資訊,根本沒有上傳的代碼