FTP 上传文件到远程服务器的 CURL 和 PHP 上传一个空白文件 [英] FTP upload file to distant server with CURL and PHP uploads a blank file

查看:23
本文介绍了FTP 上传文件到远程服务器的 CURL 和 PHP 上传一个空白文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将文件上传到远程服务器,但看起来源文件什么也没做.我得到的只是服务器上的一个空白文件.我的代码是这样的:

I'm trying to upload a file to a distant server, but looks like the source file does nothing. All i get is a blank file on the server. My code is this:

<?php

    $c = curl_init();
    $file = "PATHTOFILEfile.txt";
    $fp = fopen($file, "r");

    curl_setopt($c, CURLOPT_URL, "SERVERPATH/file.txt");
    curl_setopt($c, CURLOPT_USERPWD, "USER:PASSWORD");
    curl_setopt($c, CURLOPT_UPLOAD, 1);
    curl_setopt($c, CURLOPT_INFILE, $fp);
    curl_setopt($c, CURLOPT_INFILESIZE, filesize($file));

    curl_exec($c);
      echo "Success";
    curl_close($c);
    fclose($fp); 

?>

推荐答案

在用头敲击键盘 2 天后.. 终于我做到了..方法如下:

After 2 days of banging my head against the keyboard.. finally i did it.. Here is how:

<?php

    if (isset($_POST['Submit'])) {
 if (!empty($_FILES['upload']['name'])) {
    $ch = curl_init();
    $localfile = $_FILES['upload']['tmp_name'];
    $fp = fopen($localfile, 'r');
    curl_setopt($ch, CURLOPT_URL, 'ftp://domain.com/'.$_FILES['upload']['name']);
    curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_INFILE, $fp);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
    curl_exec ($ch);
    $error_no = curl_errno($ch);
    curl_close ($ch);
        if ($error_no == 0) {
            $error = 'File uploaded succesfully.';
        } else {
            $error = 'File upload error.';
        }
 } else {
        $error = 'Please select a file.';
 }
}
  echo $error;  
?> 

这是来源,我从那里找到了解决方案

Here is the source, from where i found the solution

这篇关于FTP 上传文件到远程服务器的 CURL 和 PHP 上传一个空白文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆