本文主要和大家分享php文件上传需要注意哪些事项,希望能帮助到大家。
php版本区别:
<=5.4 curl上传文件只支持@语法= 5.5 支持@语法和CURLFile类大于=5.6 只支持CURLFile类
// 兼容性写法参考示例
$curl = curl_init();if (class_exists('\CURLFile')) {// 这里用特性检测判断php版本 curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true); $data = array('file' => new \CURLFile(realpath($source)));//>=5.5 } else { if (defined('CURLOPT_SAFE_UPLOAD')) { curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false); } $data = array('file' => '@' . realpath($source));//<=5.5 } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, 1 ); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_USERAGENT,"TEST"); $result = curl_exec($curl); $error = curl_error($curl);
php版本区别:
<=5.4 curl上传文件只支持@语法= 5.5 支持@语法和CURLFile类大于=5.6 只支持CURLFile类
// 兼容性写法参考示例
$curl = curl_init();if (class_exists('\CURLFile')) {// 这里用特性检测判断php版本 curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true); $data = array('file' => new \CURLFile(realpath($source)));//>=5.5 } else { if (defined('CURLOPT_SAFE_UPLOAD')) { curl_setopt($curl, CURLOPT_SAFE_UPLOAD, false); } $data = array('file' => '@' . realpath($source));//<=5.5 } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, 1 ); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_USERAGENT,"TEST"); $result = curl_exec($curl); $error = curl_error($curl);
相关推荐:
Atas ialah kandungan terperinci php文件上传需要注意哪些. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!