Code to upload Video from Android to PHP -
i trying upload video android code php web server code can't success. referring following link perform uploading task getting following response in android code, can't found file on php server.
android response
debug/servercode(29484): 200 debug/serverresponsemessage(29484): ok
i have checked many things setting values in php.ini
files. although can upload images android code server becasue android sending 64 bit encoded bytearray
, therer ready-made function in php can create image encoded bytearray
s.
but code not working in case of other file having type other image.
please guide me if of have done similar before.
php code using:
<?php $target_path = "./upload/"; $target_path = $target_path . basename( $_files['uploadedfile']['name']); if(move_uploaded_file($_files['uploadedfile']['tmp_name'], $target_path)) { echo "the file ".basename( $_files['uploadedfile']['name'])." has been uploaded"; } else { echo "there error uploading file, please try again!"; } ?>
android code using:
public void videoupload() { httpurlconnection connection = null; dataoutputstream outputstream = null; datainputstream inputstream = null; string pathtoourfile = "/sdcard/video-2010-03-07-15-40-57.3gp"; string urlserver = "http://10.0.0.15/sampleweb/handle_upload.php"; string lineend = "\r\n"; string twohyphens = "--"; string boundary = "*****"; int bytesread, bytesavailable, buffersize; byte[] buffer; int maxbuffersize = 1*1024*1024; try { fileinputstream fileinputstream = new fileinputstream(new file(pathtoourfile) ); url url = new url(urlserver); connection = (httpurlconnection) url.openconnection(); // allow inputs & outputs connection.setdoinput(true); connection.setdooutput(true); connection.setusecaches(false); // enable post method connection.setrequestmethod("post"); connection.setrequestproperty("connection", "keep-alive"); connection.setrequestproperty("content-type", "multipart/form-data;boundary"); outputstream = new dataoutputstream( connection.getoutputstream() ); outputstream.writebytes(twohyphens + boundary + lineend); outputstream.writebytes("content-disposition: form-data; name=\"uploadedfile\";filename=\"" + pathtoourfile ); outputstream.writebytes(lineend); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); buffer = new byte[buffersize]; // read file bytesread = fileinputstream.read(buffer, 0, buffersize); while (bytesread > 0) { outputstream.write(buffer, 0, buffersize); bytesavailable = fileinputstream.available(); buffersize = math.min(bytesavailable, maxbuffersize); bytesread = fileinputstream.read(buffer, 0, buffersize); } outputstream.writebytes(lineend); outputstream.writebytes(twohyphens + boundary + twohyphens + lineend); // responses server (code , message) int serverresponsecode = connection.getresponsecode(); string serverresponsemessage = connection.getresponsemessage(); log.d("servercode",""+serverresponsecode); log.d("serverresponsemessage",""+serverresponsemessage); fileinputstream.close(); outputstream.flush(); outputstream.close(); } catch (exception ex) { ex.printstacktrace(); } }
my java bit ropey but....
it looks connection.getresponsecode returning http status code, , connection.getresponsemessage returning http status message - you're php nothing manipulate values. might try:
$target_path = "./upload/"; $src = $_files['uploadedfile']['name']; $target_path .= basename($src); if(file_exists($src) && && move_uploaded_file($src, $target_path) ) { echo "the file ".basename( $_files['uploadedfile']['name'])." has been uploaded"; } else { header("server error", true, 503); echo "there error uploading file, please try again!"; $msg = "src size ? " . filesize($src) . "\n dest dir writable ?" . is_writeable(dirname($target_path)) ? "y\n" : "n\n" . "files contains :\n"; . var_export($_files,true); // write $msg somwhere can read }
this shuld in narrowing down went wrong
Comments
Post a Comment