CURL通过FTP上传空白文件

时间:2012-01-26 18:52:38

标签: php curl

我正在使用此curl脚本尝试通过FTP上传用户选择的文件。它将文件上传到服务器,但它们都是空白的。为什么会这样?

if (!empty($_FILES['userfile']['name'])) {
    $ch = curl_init();
    $localfile = $_FILES['upload']['tmp_name'];
    $fp = fopen($localfile, 'r');
    curl_setopt($ch, CURLOPT_URL, 'ftp-addy-here'.$_FILES['userfile']['name']);
    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;

2 个答案:

答案 0 :(得分:4)

第3行,现在说:

$localfile = $_FILES['upload']['tmp_name'];

将其更改为:

$localfile = $_FILES['userfile']['tmp_name'];

答案 1 :(得分:1)

您没有检查初始文件上传是否成功。检查是否存在远程文件名 NOT 表示已上传。确保上传工作的100%可靠方法是

if ($_FILES['userfile']['error'] === UPLOAD_ERR_OK) {
  ... ftp stuff ...
} else {
    die("Upload failed with error code: {$_FILES['userfile']['error']}");
}

代码/常量记录在此处:http://php.net/manual/en/features.file-upload.errors.php