我和我的朋友有一個問題,當涉及到從Android應用程序上傳到Codeigniter框架的圖片在PHP。問題出現了(我們認爲),因爲它不是圖像文件,因此不能用於圖像操作。從Android的Java應用程序上傳圖片Codeigniter
一個很大的問題是,使用html頁面進行測試時,圖片上傳工作正常。並且所有其他數據(如電子郵件)都在android應用程序中提供並運行。唯一不起作用的是文件上傳。但它可以與另一個php文件一起工作,但是我的問題是如何將最後一個php腳本運行並將其轉換爲codeigniter?我想用這個框架,但到目前爲止我們還沒有做到。
下面是上傳的javacode - >
HttpClient httpClient = new DefaultHttpClient();
HttpContext httpContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost("http://url.com/controller-name");
try
{
CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(new ProgressListener()
{
@Override
public void transferred(long num)
{
publishProgress((int) ((num/(float) totalSize) * 100));
}
});
// We use FileBody to transfer an image
multipartContent.addPart("userfile", new FileBody(new File(filename)));
totalSize = multipartContent.getContentLength();
multipartContent.addPart("email", new StringBody("[email protected]"));
multipartContent.addPart("submit", new StringBody("upload"));
// Send it
httpPost.setEntity(multipartContent);
HttpResponse response = httpClient.execute(httpPost, httpContext);
String serverResponse = EntityUtils.toString(response.getEntity());
Log.i("SERVER", "Response: " + response.toString());
return serverResponse;
}
catch (Exception e)
{
System.out.println(e);
}
,用來上傳的笨方法:
function do_upload() {
$image_name = time() . $this->get_random_string();
$config = array(
'file_name' => $image_name,
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->gallery_path
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
$config = array(
'source_image' => $image_data['full_path'],
'new_image' => $this->gallery_path . '/thumbs',
'maintain_ratio' => false,
'width' => 300,
'height' => 300
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
echo $this->image_lib->display_errors();
$image_name = $image_name . $image_data['file_ext'];
//Insert into the database
$data = array(
'image_name' => $image_name,
'upload_email' => $this->input->post('email'),
'ip' => $this->input->ip_address()
);
// Insert the new data of the image!
$insert = $this->db->insert('table', $data);
$short_url = $this->alphaID($this->db->insert_id());
return $short_url;
}
年長的PHP腳本的作品,注意的是,註釋代碼不起作用。因爲它可能會產生與CI相同的效果。
<?php
/*if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "application/octet-stream")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{*/
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"]/1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("images/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/" . $_FILES["file"]["name"]);
echo "Stored in: " . "images/" . $_FILES["file"]["name"];
}
}
/*}
else
{
echo "Invalid file";
}*/
?>
請幫助我們修改CI函數或java代碼,以便我們可以上傳我們心愛的圖像。感謝您的建議和更好的智慧!
如果添加echo $ this-> upload-> display_errors(),應用程序將輸出「No file selected」,就好像它沒有任何文件首先! :( – Ms01 2012-03-12 09:00:33
更改了一下java代碼,得到了這個:http://pastie.org/private/v7dpbdofhfn97znggslha – Ms01 2012-03-12 21:25:33