0

I am trying to implement the file upload module.

I have tested the php server part using html , it is correct .

<?php
error_reporting(E_ALL);

    $target_path  = "target/";
  //  $target_path = $target_path . basename( $_FILES['file']['name']);
    $name = $_FILES["file"]["name"];
    echo $name."<br>";
    $ext = end(explode(".", $name));
        echo $ext."<br>";
    $randname = random_string(30);
    echo $randname."<br>";
    $fname = $randname . "." .$ext;
    echo $fname."<br>";
    $target_path = $target_path .$fname;
    echo $target_path."<br>";
    if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
        //echo "The file ".  basename( $_FILES['file']['name'])." has been uploaded";
        //echo random_string(50);



        // $arr = array ('data'=>'http://gallicalab.com/target/'.$_FILES['file']['name']);
        $arr = array ('data'=>'http://gallicalab.com/target/'.$fname);

        echo json_encode($arr); // {"a":1,"b":2,"c":3,"d":4,"e":5}
    } 
    else {
        echo "There was an error uploading the file, please try again!";
    }



    function random_string($length) {
    $key = '';
    $keys = array_merge(range(0, 9), range('a', 'z'));

    for ($i = 0; $i < $length; $i++) {
        $key .= $keys[array_rand($keys)];
    }

    return $key;
    }

?>

When it comes to android , it fails..

public String uploadRecording(File frecord ) throws FileNotFoundException {
    // TODO Auto-generated method stub
    String destinationPath = "http://www.gallicalab.com/upload.php";        

     System.out.println("step 3");
    String result = "";
    HttpClient client=new DefaultHttpClient();
    HttpPost post=new HttpPost(destinationPath);

 System.out.println("step 4");
try{
    //setup multipart entity
    MultipartEntity entity=new MultipartEntity();

        FileBody fileBody=new FileBody(frecord);
         System.out.println("step 5");
        entity.addPart("file", fileBody);



    post.setEntity(entity);


     System.out.println("step 5.5");
    HttpResponse uploadReponse = client.execute(post);
    Log.d("debug" , "Response : " + uploadReponse);
    Log.d("debug" , "Response 2: " + uploadReponse.getStatusLine().getReasonPhrase());
    Log.d("debug" , "Response 3: " + uploadReponse.getStatusLine().getStatusCode());

     System.out.println("step 6");
    if(uploadReponse.getStatusLine().getStatusCode() == 200){
        InputStream isInputStream = uploadReponse.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(    isInputStream, "utf-8"), 8192);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }

        result =   sb.toString();  
    }
}catch(Exception e){
    e.printStackTrace();
}     
return result;  

}

And showing Undefine index in file line 6

19
  • think here is the problem $name = $_FILES["file"]["tmp_name"]; Commented Jan 16, 2014 at 13:11
  • code modified , but the problem still persists Commented Jan 16, 2014 at 13:14
  • try a var_dump on $_FILES["file"] and check if ["name"] exists Commented Jan 16, 2014 at 13:26
  • I have used html upload code to check . ["name"] exists. But when it comes to android upload using apache htttp mime entity to upload, it does not exist and shows NULL Commented Jan 16, 2014 at 13:32
  • have a look at this stackoverflow.com/questions/17057029/… Commented Jan 16, 2014 at 13:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.