DoubleLi

qq: 517712484 wx: ldbgliet

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

解决了什么问题:curl在使用各种方式上传文件到服务器。
一般的文件上传是通过html表单进行的,通过CURL可以不经过浏览器,直接在服务器端模拟进行表单提交,完成POST数据、文件上传等功能。

服务器端PHP代码:

<?php
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 200000)) {
    if ($_FILES["file"]["error"] > 0) {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    } else {
        if (file_exists("upload/" . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
        } else {
            move_uploaded_file($_FILES["file"]["tmp_name"], "data/" . $_FILES["file"]["name"]);
            // echo "Stored in: " . "data/" . $_FILES["file"]["name"];
            echo "文件上传成功!";
        }
    }
} else {
    echo " 文件无效!";
}
?>

客户端提交的form:

<form action="upload.php" method="post" enctype="multipart/form-data">
                        <input type="file" name="file"/>
                        <input type="submit" class="btn btn-primary btn-large" value="上传文件" />
  </form>


在控制台使用curl上传的例子:

curl -F "action=upload.php" -F "method=post" -F "file=@/Users/mark/xx.gif" http://ylg.zs108.com:8080/tmp/upload.php


使用libcurl实现的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>

#define POSTURL "http://ylg.zs108.com:8080/tmp/upload.php"
#define POSTFIELDS "?action=upload&method=post"
#define FILENAME "curlposttest.log"

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp);

int main(int argc, char *argv[]) {
    CURL *curl;
    CURLcode res;
    FILE *fptr;
    struct curl_slist *http_header = NULL;
    
    if ((fptr = fopen(FILENAME, "w")) == NULL) {
        fprintf(stderr, "fopen file error: %s\n", FILENAME);
        exit(1);
    }
    
    curl = curl_easy_init();
    
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, fptr);
    
    struct curl_httppost *formpost = 0;
    struct curl_httppost *lastptr  = 0;
    curl_formadd(&formpost, &lastptr, CURLFORM_PTRNAME, "reqformat", CURLFORM_PTRCONTENTS, "plain", CURLFORM_END);
    curl_formadd(&formpost, &lastptr, CURLFORM_PTRNAME, "file", CURLFORM_FILE, "/Users/hanyanyan/xx.gif", CURLFORM_END);
    curl_easy_setopt(curl, CURLOPT_URL, POSTURL);
    curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
    
    res = curl_easy_perform(curl);
    curl_easy_cleanup(curl);
}

size_t write_data(void *buffer, size_t size, size_t nmemb, void *userp) {
    FILE *fptr = (FILE*)userp;
    fwrite(buffer, size, nmemb, fptr);
}  

编译方式:gcc -o curl_post_file filename.c -lcurl

后期调整:
<!DOCTYPE html>
<!-- saved from url=(0044)http://ylg.zs108.com:8080/dp/uploadtest.html -->
<html lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta charset="UTF-8">
        <title>Document</title>
    <style type="text/css"></style></head>
    <body>
        <div class="containter-fluid">
            <div class="row-fluid">
                <div class="span8 offset2">
                    
                    <form action="http://ylg.zs108.com:8080/dp/upload.php" method="post" enctype="multipart/form-data">
                        <table cellpadding="10" border="1">
                            <tbody><tr>
                                <td>wid</td>
                                <td><input type="text" name="wid" value="dengpan"></td>
                            </tr>
                            <tr>
                                <td>text</td>
                                <td><input type="text" name="text" value="哈哈哈呵呵"></td>
                            </tr>
                            <tr>
                                <td>stime</td>
                                <td><input type="text" name="stime" value="13"></td>
                            </tr>
                            <tr>
                                <td>ssize</td>
                                <td><input type="text" name="ssize" value="15"></td>
                            </tr>
                            <tr>
                                <td>lid</td>
                                <td><input type="text" name="lid" value="2"></td>
                            </tr>
                            <tr>
                                <td>type</td>
                                <td><input type="text" name="type" value="2"></td>
                            </tr>
                            <tr>
                                <td>sound</td>
                                <td><input type="file" name="sound"></td>
                            </tr>
                            <tr>
                                <td></td>
                                <td><input type="submit" value="提交"></td>
                            </tr>
                        </tbody></table>
                    </form>
                </div>
            </div>
        </div>
    

</body></html>

命令行:
curl -F "method=post" -F "action=upload.php" -F "wid=dengpan" -F "text=hh" -F "stime=13" -F "ssize=15" -F "lid=2" -F "type=2" -F "sound=@/Users/hanyanyan/million.amr" http://ylg.zs108.com:8080/dp/upload.php
返回代码:
{"code":1,"result":{"sound":"http:\/\/ylg.zs108.com:8080\/dp\/data\/sounds\/1378815106-a3864fc57553bf4b299b4d4c0ce07649.amr"}}
最后在贴一段PHP上传的代码吧
需要被上传的文件需要在文件名前加上“@”以示区分,并且,文件名需要是完整路径。
以下php函数来模拟html表单的提交数据:
function uploadByCURL($post_data,$post_url){
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $post_url);
    curl_setopt($curl, CURLOPT_POST, 1 );
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl,CURLOPT_USERAGENT,"Mozilla/4.0");
    $result = curl_exec($curl);
    $error = curl_error($curl);
    return $error ? $error : $result;
}

/*函数的使用:*/

$url = "http://127.0.0.1/app.php";
$data = array(
    "username" => $username,
    "password"  => $password,
    "file1"  => "@".realpath("photo1.jpg"),
    "file2"  => "@".realpath("file2.xml")
);
print_r(uploadByCURL($data,$url));
posted on 2015-01-21 16:30  DoubleLi  阅读(7070)  评论(0编辑  收藏  举报