dongwuwei0718 2019-02-11 08:23
浏览 675
已采纳

如何使用php中的curl将文件从本地上传到服务器?

I want to upload the file from local to server using curl in php and without using the form (which is html).

My php version is 5.2.6.

I have tried many different way and make many research about (upload file to server using curl in php), but the solution in google cannot solve my problem.

Below is my code:

// open file descriptor
$fp = fopen ("user.com/user/fromLocal.txt", 'w+') or die('Unable to write a file'); 

// file to download
$ch = curl_init('C:/wamp/www/user/fromLocal.txt');
// enable SSL if needed
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 

// output to file descriptor
curl_setopt($ch, CURLOPT_FILE, $fp);          
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
// set large timeout to allow curl to run for a longer time
curl_setopt($ch, CURLOPT_TIMEOUT, 1000);     
curl_setopt($ch, CURLOPT_USERAGENT, 'any');
// Enable debug output
curl_setopt($ch, CURLOPT_VERBOSE, true);

echo $ch;
echo $fp;
curl_exec($ch);
curl_close($ch);                               
fclose($fp);

Expected output:

The file can upload to server and view.

Actual output:

Unable to write the file

  • 写回答

1条回答 默认 最新

  • dongyi1921 2019-02-11 08:28
    关注

    I think you miss important information.

    fopen ("user.com/user/fromLocal.txt", 'w+')
    

    this means nothing. To send a file to the server the server has to be configured to accept a POST request and you have to send the file through that endpoint.

    If you have a form, do you send it to: "user.com/user/fromLocal.txt" ?? You have to create the form data with curl and send it to a server ready to accept your request. There are different ways to accomplish that. And the most simple is exactly to send a form using curl and not the HTML. But absolutly you cannot write a file like that in a server.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵