脚本宝典收集整理的这篇文章主要介绍了php – 使用CURLOPT_POSTFIELDS POST文件时文件为空,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
$filename = "pathtofile/testfile.txt"; $handle = foPEn($filename,"r"); $filecontents = fread($handle,filesize($filename)); fclose($handle); $data = array('name' => 'testfile.txt','file' => $filecontents); $client = curl_init($url); curl_setopt($client,CURLOPT_POST,true); curl_setopt($client,CURLOPT_POSTFIELDS,$data); curl_setopt($client,CURLOPT_RETURNtransfer,1); curl_close($client);
但我保持gettig文件为空,作为对此请求的响应.
我也试过发送文件路径,如:
$data = array('name' => 'testfile.txt','file' => 'pathtofile/testfile.txt'); curl_setopt($client,$data);
curl_setopt($client,$filecontents);
但是同样的反应:文件是空的.
请注意:文件存在且不为空,我只是尝试仅上传文件而没有其他字段.
我看到this post,但同样的问题,任何想法?
$data = array ('myfile' => '@'.$filename);
这将为接收端填充$_FILE [‘myfile’].
//Get the file data $body = file_get_contents ($filename); $len = strlen ($body); //Open a direct connection to the server on port 80 $socket = fsockopen ('hostname.example.COM',80); //WrITe the HTTP request headers fwrite ($socket,"POST /path/to/url HTTP/1.1\r\n"); fwrite ($socket,"Host: hostname.example.com\r\n"); fwrite ($socket,"Connection: Close\r\n"); fwrite ($socket,"Content-Length: " . $len . "\r\n"); //Empty line marks end of headers,start of body fwrite ($socket,"\r\n"); //Actually write the body fwrite ($socket,$body); //Get the result (half a kB at a time) $result = ''; while (!feof ($socket)) $result .= fread ($socket,512); //Clean up nicely fclose ($socket);
以上是脚本宝典为你收集整理的php – 使用CURLOPT_POSTFIELDS POST文件时文件为空全部内容,希望文章能够帮你解决php – 使用CURLOPT_POSTFIELDS POST文件时文件为空所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。