dongyi6269
dongyi6269
2017-07-05 12:06

如何使用ftp连接通过PHP cURL上传文件?

已采纳

I am using this script to upload myfile by curl function and ftp connection.

In local it works fine but in my server file is appeared uploaded but it have zero file size.

what is wrong? Thank You.

$ch = curl_init();
$localfile = (dirname(__FILE__).'/asset/myfile.zip');
$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:$user_pass@$server/".'myfile.zip');
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
curl_close ($ch);

and also how can I upload multiple files in this script like:

$localfile1 = (dirname(__FILE__).'/asset/myfile1.zip');
$localfile2 = (dirname(__FILE__).'/asset/myfile2.zip');
$localfile3 = (dirname(__FILE__).'/asset/myfile3.zip');
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • douxian1892 douxian1892 4年前

    To upload the file in curl you can use the curl_file_create.Try the below one:

    $localfile = (dirname(__FILE__).'/asset/myfile.zip');
    $curl_file = curl_file_create($localfile,'zip');
    $params = ['file' => $curl_file];
    
    $ch = curl_init();
    $localfile = (dirname(__FILE__).'/asset/myfile.zip');
    $fp = fopen($localfile, 'r');
    curl_setopt($ch, CURLOPT_URL, "ftp://$user_name:$user_pass@$server/".'myfile.zip');
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    //curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
    curl_exec ($ch);
    curl_close ($ch);
    
    点赞 评论 复制链接分享
  • doudanglang5826 doudanglang5826 4年前
    点赞 评论 复制链接分享

相关推荐