卡住了-通过Curl上传数据二进制选项

时间:2018-11-24 22:57:17

标签: php curl post file-upload

首先我要说的是,我对cURL完全陌生。但是我只是用curl和PHP编写了一些代码,以上传一些XML文件。 以下代码效果很好。 (只是环顾四周并建立一个简单的工作解决方案)

现在我遇到了使用curl --Data-Binary选项而不是-F(Form)选项的问题。

以下代码位于php文件(curlupload.php)中:

<?php

@$authcode = htmlspecialchars($_POST["acode"]);

if ($authcode == "1234"){
echo "AuthCode ok\n";

$upstatus = "1";
$uploadpath = "files/";
$filedata = $_FILES['file']['tmp_name'];
$target_file = $uploadpath . basename($_FILES["file"]["name"]);

#echo "Acode:" . $authcode . "\n";
#echo "filedata= ".$filedata."\n";
echo "target_file= ".$target_file."\n";
#echo "1 upstatus= ".$upstatus."\n";

if (strpos($target_file, ".xml") == true) {
    $upstatus = "1";
    echo "2 upstatus= ".$upstatus."\n";

    if (file_exists($target_file)) {
        echo "Sorry, file already exists.\n";
        $upstatus = "0";
    }

    #echo "4 upstatus= ".$upstatus."\n";
    if ($upstatus == "1") { 
        if ($filedata != '')
        copy($filedata,$target_file);
        echo "\n-----\n";
        echo "success\n";
    }

}else{
    $upstatus = "0";
    #echo "3 upstatus= ".$upstatus."\n";
    echo "Filenamecheck Failed.\n";
}



if ($upstatus == "0") {
    echo "Sorry, your file was not uploaded.\n";
} 
 }else{
  echo "NO.";
}
?>

我的用于上传文件的cURL命令如下:

curl -v -F 'file=@/root/testfile01.xml' -F 'acode=1234' http://webserver/upload2/curlupload.php

现在,我将使用-d和--data-binary选项,如下所示:

curl --data-binary "file=@testfile02.xml" --data "acode=1234" http://webserver/upload2/curluploadtest.php

使用上面的第二个命令,我收到以下消息:

AuthCode ok
target_file= files/
Filenamecheck Failed.
Sorry, your file was not uploaded.

因此,变量没有输入-有人可以告诉我,我在PHP文件中需要进行哪些更改,以便curl使用的databinary选项可以正常工作吗?

非常感谢您的帮助!

0 个答案:

没有答案