php - File is not being uploaded properly -
in android app, i've upload file server , read file @ server side. file being uploaded times happen, in folder (at server in file resides) if uploaded file exists, current uploaded file generated previous file data. e.g. if upload file named a server data '1, 2 , 3' there file a.txt
under folder @ server side same data, , if upload file named b server data 'a, b , c' file b.txt
under folder there previous file data of a.txt
. in b.txt
, data '1, 2 , 3'. below code. can issue?
android side
public void uploaduserfriendid(string user_id, string filepath, string filename) { string server_url = "http://addressofserver/folder/myfile.php"; inputstream inputstream; try { inputstream = new fileinputstream(new file(filepath)); byte[] data; try { data = ioutils.tobytearray(inputstream); httpclient httpclient = new defaulthttpclient(); httpclient.getparams().setparameter(coreprotocolpnames.user_agent, system.getproperty("http.agent")); httppost httppost = new httppost(server_url); inputstreambody inputstreambody = new inputstreambody(new bytearrayinputstream(data), filename); multipartentity multipartentity = new multipartentity(); multipartentity.addpart("file", inputstreambody); multipartentity.addpart("user_id", new stringbody(user_id)); httppost.setentity(multipartentity); httpresponse httpresponse = httpclient.execute(httppost); // handle response script. if(httpresponse != null) { //toast.maketext(getbasecontext(), "upload completed. ", 2000).show(); } else { // error, no response. //toast.maketext(getbasecontext(), "server error. ", 2000).show(); } } catch (ioexception e) { e.printstacktrace(); } } catch (filenotfoundexception e1) { e1.printstacktrace(); } }
php side
<?php error_reporting(~0); ini_set('display_errors', 1); mysql_connect("localhost", "root", "admin@123") or die(mysql_error()) ; mysql_select_db("retail_menu") or die(mysql_error()) ; $today =date("ymdhis"); $pic=$today.".txt"; if (isset($_post["user_id"]) && !empty($_post["user_id"])) { $user_id=$_post['user_id']; } else { $user_id="null"; } $objfile = & $_files["file"]; // here file created under folder named upload @ server side if( move_uploaded_file( $objfile["tmp_name"], "upload/".$user_id.".txt" ) ) { $file = fopen("upload/".$user_id.".txt" ,"r"); while(! feof($file)) { $friend_id = fgets($file, 8192); if($friend_id!= null) { $query = "insert ignore table_name (user_id, friend_id) values('$user_id', '$friend_id')"; var_dump($query); mysql_query($query); } } fclose($file); } else { print "there error uploading file, please try again!"; } ?>
Comments
Post a Comment