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.

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

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图