用curl上传文件的话很方便,什么header,post串都不用生成了,用fsockopen要写一堆
curl:
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);
本文介绍了如何使用PHP中的curl与fsockopen两种方式实现文件上传功能。通过curl可以简化HTTP请求过程,直接上传文件而无需手动构造header及POST数据;而使用fsockopen则需要详细设置HTTP头部信息及请求正文。
141

被折叠的 条评论
为什么被折叠?



