不同PHP版本上传文件写法对比
原创
52cxy
08-29 17:57
阅读数:254
PHP<5.50 的写法,文件路径前加上"@"即可,
$data = array('file'=>"@{filePath}"];
PHP>=5.50 的写法,需要new一个CURLFile对像,
$data = array('file'=>new CURLFile(realpath($filePath))];
完整示例代码如下:
PHP5.5
$reqUrl = '请求地址'; $filePath = '文件路径'; $curl = curl_init(); $data = array('file'=>"@{filePath}"]; curl_setopt($curl,CURLOPT_URL, $reqUrl); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl,CURLOPT_POST, true); curl_setopt($curl,CURLOPT_POSTFIELDS, $data); curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($ch);
PHP7:
$reqUrl = '请求地址'; $filePath = '文件路径'; $curl = curl_init(); $data = array('file'=>new CURLFile(realpath($filePath))]; curl_setopt($curl,CURLOPT_URL, $reqUrl); curl_setopt($ch, CURLOPT_FAILONERROR, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl,CURLOPT_POST, true); curl_setopt($curl,CURLOPT_POSTFIELDS, $data); curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($curl); curl_close($ch);
共0条评论