using php's libcurl to register user and upload file to server

Posted by tunpishuang on Stack Overflow See other posts from Stack Overflow or by tunpishuang
Published on 2010-06-12T13:50:55Z Indexed on 2010/06/12 14:22 UTC
Read the original article Hit count: 425

Filed under:
|

here is a site http://www.lyrkjsw.gov.cn that can let the registered user to upload file (e.g. images or office files) to the site.

i want to register user and upload image to this site using libcurl binding with php.

only registered user can upload image. so i use cookiejar stored in c:\cookie.txt after register and use c:\cookie.txt in uploadImg() function .

register user is valid but failed to upload image , can anybody know is there any mistake of my code:

<?
/*
    options
*/
//the list url
$expUrl='http://www.lyrkjsw.gov.cn/hbcms/user/list_resource.php';
//the  user info to be registered
$regUser='jiong';
$regPass='jiong';
$regMail='[email protected]';
$regUrl=str_replace('list_resource.php','register.php',$expUrl);
// options for image upload
$fileDir='@D:\img\b.jpg';
$fileTitle='aaaaaaaaaaaaa';
$fileDesc='aaaaaaaaaaaaadesc';
$uploadImgUrl=str_replace('list_resource.php','add_resource.php',$expUrl);
/*
    register function
*/
function reg($regurl,$u,$p,$m)
{
        $ch = curl_init();
        $options=array(
            CURLOPT_URL=>$regurl,
            CURLOPT_RETURNTRANSFER=>true,
            CURLOPT_POST=>true,
            CURLOPT_POSTFIELDS=>'mod=register_now&next_url=index.php&addon_app=&referrer_id=&login_name='.$u.'&login_pass='.$p.'&confirm_login_pass='.$p.'&login_email='.$m.'&nickname=&gender=0&qq=&mobile=&telephone=&true_name=&website_name=&website_url=&my_question=&my_answer=',
            CURLOPT_COOKIESESSION=>true,
            CURLOPT_HEADER=>true,
            CURLOPT_COOKIEJAR=>'c:\cookie.txt'
        );
        curl_setopt_array($ch,$options);
        $data = curl_exec($ch);
        if(strpos($data,'??')){
            printf("register ok :)\n");
            curl_close($ch);
            return true;
        }else{
            printf("register failed:(\n");
            curl_close($ch);
            return false;
        }
}
/*
    image uploading function
*/
function uploadImg($uploadimgurl,$filedir,$filetitle,$filedesc)
{
        $ch = curl_init();
        $options=array(
            CURLOPT_COOKIEFILE=>'c:\cookie.txt',
            CURLOPT_URL=>$uploadimgurl,
            CURLOPT_RETURNTRANSFER=>1,
            CURLOPT_POST=>1,
            CURLOPT_POSTFIELDS=>"                   
                'MAX_FILE_SIZE'='33554432'&
                'preview_area_id'='upload_file'&
                'editor_area_id'='body'&
                'js_function'=''&
                'resource_id'=''&
                'show_top_part'='no'&
                'file_1'=$filedir&
                'file_title_1'=$filetitle&
                'file_desc_1'=$filedesc
                "
        );
        curl_setopt_array($ch,$options);
        $data = curl_exec($ch);
        if(strpos($data,'??')){
            printf("upload ok :)\n");
        }else{
            printf("upload failed :(\n");
        }
        curl_close($ch);

}

if(reg($regUrl,$regUser,$regPass,$regMail) != false)
{
    uploadImg($uploadImgUrl,$fileDir,$fileTitle,$fileDesc);
}

http://www.lyrkjsw.gov.cn/hbcms/user/list_resource.php (list file page) http://www.lyrkjsw.gov.cn/hbcms/user/register.php (register page) http://www.lyrkjsw.gov.cn/hbcms/user/add_resource.php (image uploading page)

© Stack Overflow or respective owner

Related posts about php

Related posts about libcurl