In the past few days, I have to transfer files because I need to call the java interface. I chose curl to transfer files. One pitfall here is that I used the original @ followed by the absolute path of the file directly when transferring files.
However, there was no error, but it felt like it was never sent. Later, I tried it under win, and a warning was thrown directly saying that this method is outdated, please use the curlfile function instead. Only now did I realize that my computer had never turned on the warning and reported errors, but I had always turned it on under win. I had been struggling with this problem for some time.
Let’s look at the code below to see how curl transfers files and array data,
$beforePath = '/Applications/MAMP/htdocs/photo/1.jpg'; $afterPath = '/Applications/MAMP/htdocs/photo/2.jpg';//绝对路径 $data = [ 'top' => curl_file_create($beforePath), 'after' => curl_file_create($afterPath), 'partnerId' => $data['0']['uid'], 'mobile' => $data1['0']['phone'], 'email' => $data1['0']['email'], 'realName' => $data['0']['real_name'], ]; curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //禁用证书 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch,CURLOPT_BINARYTRANSFER,true); curl_setopt($ch, CURLOPT_POSTFIELDS,$data); curl_setopt($ch, CURLOPT_URL, $url); $info= curl_exec($ch); curl_close($ch);
The above introduces the high and low versions of curl to transfer files, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.