php - Android multipart entity image upload error -
i having problem when uploading image server. when upload http 1.1 200 ok when wath response see file not uploaded because file of same name exists or not valid type. here async task , php server code.
private class uploadtask extends asynctask<bitmap, void, void> { protected void doinbackground(bitmap... bitmaps) { if (bitmaps[0] == null) return null; setprogress(0); bitmap bitmap = bitmaps[0]; bytearrayoutputstream stream = new bytearrayoutputstream(); bitmap.compress(bitmap.compressformat.png, 100, stream); // convert bitmap bytearrayoutputstream inputstream in = new bytearrayinputstream(stream.tobytearray()); // convert bytearrayoutputstream bytearrayinputstream defaulthttpclient httpclient = new defaulthttpclient(); try { httppost httppost = new httppost( "http://mapped.guri.sk/imageupload"); // server multipartentity reqentity = new multipartentity(); reqentity.addpart("myfile", system.currenttimemillis() + ".jpg", in); httppost.setentity(reqentity); log.i("tag", "request " + httppost.getrequestline()); try { response = httpclient.execute(httppost); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { if (response != null) { log.i("tag", "response " + response.getstatusline().tostring()); try { responsebody = entityutils.tostring(response.getentity()); } catch (ioexception e) { e.printstacktrace(); } } } { } } { } if (in != null) { try { in.close(); } catch (ioexception e) { e.printstacktrace(); } } if (stream != null) { try { stream.close(); } catch (ioexception e) { e.printstacktrace(); } } return null; } @override protected void onprogressupdate(void... values) { super.onprogressupdate(values); } @override protected void onpostexecute(void result) { super.onpostexecute(result); toast.maketext(getapplicationcontext(), "response: " + responsebody, toast.length_long).show(); //toast.maketext(mainactivity.this, r.string.uploaded, toast.length_long).show(); } }
and php:
function imageupload(){ $target_dir = "/home/web/files.guri.sk/"; $target_file = $target_dir . basename($_files["filetoupload"]["name"]); $uploadok = 1; $imagefiletype = pathinfo($target_file,pathinfo_extension); // check if image file actual image or fake image if(isset($_post["submit"])) { $check = getimagesize($_files["filetoupload"]["tmp_name"]); if($check !== false) { //echo "file image - " . $check["mime"] . "."; $uploadok = 1; } else { echo "file not image."; $uploadok = 0; } } // check if file exists if (file_exists($target_file)) { echo "sorry, file exists."; $uploadok = 0; } // check file size if ($_files["filetoupload"]["size"] > 10000000) { echo "sorry, file large."; $uploadok = 0; } // allow file formats if($imagefiletype != "jpg" && $imagefiletype != "png" && $imagefiletype != "jpeg" && $imagefiletype != "gif" ) { echo "sorry, jpg, jpeg, png & gif files allowed."; $uploadok = 0; } // check if $uploadok set 0 error if ($uploadok == 0) { echo "sorry, file not uploaded."; // if ok, try upload file } else { if (move_uploaded_file($_files["filetoupload"]["tmp_name"], $target_file)) { //echo "the file ". basename( $_files["filetoupload"]["name"]). " has been uploaded."; echo "http://files.guri.sk/".basename($_files["filetoupload"]["name"]); } else { echo "sorry, there error uploading file."; } } }
Comments
Post a Comment