dongyi6269
2017-07-05 12:06
浏览 139
已采纳

如何使用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');

图片转代码服务由CSDN问答提供 功能建议

我使用此脚本通过curl函数和ftp连接上传myfile。 < p>在本地它工作正常,但在我的服务器文件中出现上传但文件大小为零。

有什么问题? 谢谢。

  $ 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  ); 
; ncurl_setopt($ ch,CURLOPT_INFILE,$ fp); 
curl_setopt($ ch,CURLOPT_INFILESIZE,filesize($ localfile)); 
curl_exec($ ch); 
curl_close($ ch); 
  <  / pre> 
 
 

以及如何在此脚本中上传多个文件,如:

  $ localfile1 =(dirname(__ FILE __)。'/ asset /  myfile1.zip'); 
 $ localfile2 =(dirname(__ FILE __)。'/ asset / myfile2.zip'); 
 $ localfile3 =(dirname(__ FILE __)。'/ asset / myfile3.zip'); 
    
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxian1892 2017-07-05 12:28
    已采纳

    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 2017-07-05 12:26
    点赞 评论

相关推荐 更多相似问题