doraemon0769 2010-06-12 13:50
浏览 39
已采纳

使用php的libcurl注册用户并将文件上传到服务器

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://hbcms/user/list_resource.php';
//the  user info to be registered
$regUser='jiong';
$regPass='jiong';
$regMail='jiong@jiong.com';
$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 :)
");
            curl_close($ch);
            return true;
        }else{
            printf("register failed:(
");
            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=>http_build_query(array(             
                '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 :)
");
        }else{
            printf("upload failed :(
");
        }
        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)

  • 写回答

1条回答 默认 最新

  • duanruoyu6675 2010-06-12 14:19
    关注

    To start with, your POSTFILEDS doesn't look correct. The single quote is not needed and the $filedir needs to be encoded.

    You should do something like this,

    $data = array(
                '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);
    $qs = http_build_query($data);
    

    And use $qs as POSTFIELDS.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求MCSCANX 帮助
  • ¥15 机器学习训练相关模型
  • ¥15 Todesk 远程写代码 anaconda jupyter python3
  • ¥15 我的R语言提示去除连锁不平衡时clump_data报错,图片以下所示,卡了好几天了,苦恼不知道如何解决,有人帮我看看怎么解决吗?
  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制