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 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据