使用Google Volley和Codeigniter上传图片

时间:2016-01-12 08:19:43

标签: php android codeigniter android-volley

我想上传图片并将图片的网址放入数据库(post_image列)。

每次尝试从Android上传图片时,我总是会收到错误500.

这是我的Android代码:

private void postTimelineData(final String post_image, final String post_description, final String post_restaurant_detail, final String user_id) {
        showProgressDialog();
        String postUrl = Utils.url + "post/insert_post/" + post_description + "/" + post_restaurant_detail + "/" + user_id;
        System.out.println("postUrl: " + postUrl);
        StringRequest strReq = new StringRequest(Request.Method.POST, postUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                System.out.println("Response: " + response);
                try {
                    hideProgressDialog();
                    Toast.makeText(Photo.this, "Your Image is posted!", Toast.LENGTH_SHORT).show();
                } catch (NullPointerException e) {
                    Utils.errorHandler(null, toolbarView, false);
                    hideProgressDialog();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Utils.errorHandler(volleyError, toolbarView, false);
                hideProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                // Posting params to register url
                Map<String, String> params = new HashMap<String, String>();
                String postUrl = Utils.url + "post/insert_post/" + post_image + "/" + post_description + "/" + post_restaurant_detail + "/" + user_id;
                params.put("post_image", post_image);
                params.put("post_description", post_description);
                params.put("post_restaurant_detail", post_restaurant_detail);
                params.put("user_id", user_id);
                return params;
            }
        };
        strReq.setRetryPolicy(new DefaultRetryPolicy(30000, 0, 1f));
        AppController.getInstance().addToRequestQueue(strReq);
    }

CI代码:

function insert_post($post_description, $post_restaurant_name, $user_id){
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '1024';
            $config['max_width']  = '1024';
            $config['max_height']  = '1024';

            $this->load->library('upload', $config);

            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());
                echo json_encode($error);
            }
            else
            {
                $upload_data = array('upload_data' => $this->upload->data());
                $post_image = base_url() .'uploads/'. $uploadData['file_name'];
                echo json_encode($post_image);
            }

            $data = $this->post_model->insert_post($post_image, $post_description, $post_restaurant_name, $user_id);
        }

谁能告诉我,如何解决这个问题?感谢您的帮助。

0 个答案:

没有答案