把php表达上传文件转化成libcrul 的curl_formadd实现,代码通过,发币!谢谢朋友们帮帮忙吧,总是对应不上! 有更好的例子也可以!

问题描述:

<?php

$header_prefix = 'file';

$slots = 6;

?>

<html>

<head>

<title>Test upload</title>

</head>

<body>

<?php

if ($_POST){

    echo "<h2>Uploaded files:</h2>";

    echo "<table border=\"2\" cellpadding=\"2\">";

    echo "<tr><td>Name</td><td>Location</td><td>Content type</td><td>MD5</td><td>Size</td><td>Scp Command</td><td>Wget Command</tr>";

    for ($i=1;$i<=$slots;$i++){

        $key = $header_prefix.$i;

        if (array_key_exists($key."_name", $_POST) && array_key_exists($key."_path",$_POST)) {

            $tmp_name = $_POST[$key."_path"];

            $name = $_POST[$key."_name"];

            $content_type = $_POST[$key."_content_type"];

            $md5 = $_POST[$key."_md5"];

            $size = $_POST[$key."_size"];

            $final_path = "/export/share/upload";

            if (copy($tmp_name, "$final_path/$name")) {

                    echo "SUCCESS!";

            } else {

                    echo "FAIL!";

            }

            $scp_cmd = "scp team@***:/export/share/upload/$name .";

            $wget_cmd = "wget http://***/files/upload/$name";

            echo "<tr><td>$name</td><td>$final_path</td><td>$content_type</td><td>$md5</td><td>$size</td><td>$scp_cmd</td><td>$wget_cmd</td>";

        }

    }

    echo "</table>";

}else{?>

<h2>Select files to upload</h2>

<form name="upload" method="POST" enctype="multipart/form-data" action="/upload">

<input type="file" name="file1"><br>

<input type="file" name="file2"><br>

<input type="file" name="file3"><br>

<input type="file" name="file4"><br>

<input type="file" name="file5"><br>

<input type="file" name="file6"><br>

<input type="submit" name="submit" value="Upload">

<input type="hidden" name="test" value="value">

</form>

<?php

}

?>

</body>

</html>

把以上的PHP动态提交表单的行为,用libcurl 中 curl_formadd 实现,感谢路过的大神伸出智慧之手!需要使用C++完成上面的实现

   // form.php

   /**
    * curl Post文件,php5以下版本可用
    * 
    * @param $action (处理上传文件的url,form表单的action)
    * @param $path (文件路径)
    **/
    function upload_file($action, $path){
        $data = array(
            // 需要注意的是,在路径前必须带上@,不然只会当做是简单的键值对
            'pic'   =>  '@'.realpath($path)
            'name'  =>  'issac'
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $action);
        curl_setopt($ch, CURLOPT_POST, true );
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  //该curl_setopt可以向header写键值对
        curl_setopt($ch, CURLOPT_HEADER, false); // 不返回头信息
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;       
    }


    // 调用
    upload_file('accrpt.php', '/files/img.png');