Uploading file in php server from android device

前端 未结 7 1703
孤独总比滥情好
孤独总比滥情好 2020-11-27 02:34

I am trying to upload file to a php server from my android device but server is not receiving any file. here is my sample code, I found it on-line. php server , I am uploa

相关标签:
7条回答
  • 2020-11-27 03:00

    If you are using free hosting then the pages are getting redirected for tracking no. of hits. You probably receive Http Response :200 OK but you wont see file uploaded because file is lost.

    I myself wasted 3 days searching for the solution but found nothing. Figured this out myself.

    0 讨论(0)
  • 2020-11-27 03:04

    Those who are looking for Kotlin solution, can use this utility class. Here is the detailed article I have written on the topic. It uses OKHTTP3. Upload File to Server in Android Kotlin

    package com.mvp.handyopinion
    
    import android.app.Activity
    import android.app.ProgressDialog
    import android.net.Uri
    import android.util.Log
    import android.webkit.MimeTypeMap
    import android.widget.Toast
    import okhttp3.*
    import okhttp3.MediaType.Companion.toMediaTypeOrNull
    import okhttp3.RequestBody.Companion.asRequestBody
    import java.io.File
    
    class UploadUtility(activity: Activity) {
    
        var activity = activity;
        var dialog: ProgressDialog? = null
        var serverURL: String = "https://handyopinion.com/tutorials/UploadToServer.php"
        var serverUploadDirectoryPath: String = "https://handyopinion.com/tutorials/uploads/"
        val client = OkHttpClient()
    
        fun uploadFile(sourceFilePath: String, uploadedFileName: String? = null) {
            uploadFile(File(sourceFilePath), uploadedFileName)
        }
    
        fun uploadFile(sourceFileUri: Uri, uploadedFileName: String? = null) {
            val pathFromUri = URIPathHelper().getPath(activity,sourceFileUri)
            uploadFile(File(pathFromUri), uploadedFileName)
        }
    
        fun uploadFile(sourceFile: File, uploadedFileName: String? = null) {
            Thread {
                val mimeType = getMimeType(sourceFile);
                if (mimeType == null) {
                    Log.e("file error", "Not able to get mime type")
                    return@Thread
                }
                val fileName: String = if (uploadedFileName == null)  sourceFile.name else uploadedFileName
                toggleProgressDialog(true)
                try {
                    val requestBody: RequestBody =
                        MultipartBody.Builder().setType(MultipartBody.FORM)
                            .addFormDataPart("uploaded_file", fileName,sourceFile.asRequestBody(mimeType.toMediaTypeOrNull()))
                            .build()
    
                    val request: Request = Request.Builder().url(serverURL).post(requestBody).build()
    
                    val response: Response = client.newCall(request).execute()
    
                    if (response.isSuccessful) {
                        Log.d("File upload","success, path: $serverUploadDirectoryPath$fileName")
                        showToast("File uploaded successfully at $serverUploadDirectoryPath$fileName")
                    } else {
                        Log.e("File upload", "failed")
                        showToast("File uploading failed")
                    }
                } catch (ex: Exception) {
                    ex.printStackTrace()
                    Log.e("File upload", "failed")
                    showToast("File uploading failed")
                }
                toggleProgressDialog(false)
            }.start()
        }
    
        // url = file path or whatever suitable URL you want.
        fun getMimeType(file: File): String? {
            var type: String? = null
            val extension = MimeTypeMap.getFileExtensionFromUrl(file.path)
            if (extension != null) {
                type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension)
            }
            return type
        }
    
        fun showToast(message: String) {
            activity.runOnUiThread {
                Toast.makeText( activity, message, Toast.LENGTH_LONG ).show()
            }
        }
    
        fun toggleProgressDialog(show: Boolean) {
            activity.runOnUiThread {
                if (show) {
                    dialog = ProgressDialog.show(activity, "", "Uploading file...", true);
                } else {
                    dialog?.dismiss();
                }
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-11-27 03:14

    These examples give an error on Nginx+HyperfastCGI+Mono. To remove the error, you need to put a space after the semicolon.

    conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
    

    Maybe someone will need this information.

    0 讨论(0)
  • 2020-11-27 03:15

    Complete working code .... with PHP code

     public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    
    private static final int PICK_FILE_REQUEST = 1;
    private static final String TAG = MainActivity.class.getSimpleName();
    private String selectedFilePath;
    private String SERVER_URL = "http://Ip Address/a.php";
    ImageView ivAttachment;
    Button bUpload;
    TextView tvFileName;
    ProgressDialog dialog;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ivAttachment = (ImageView) findViewById(R.id.ivAttachment);
        bUpload = (Button) findViewById(R.id.b_upload);
        tvFileName = (TextView) findViewById(R.id.tv_file_name);
        ivAttachment.setOnClickListener(this);
        bUpload.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        if(v== ivAttachment){
    
            //on attachment icon click
            showFileChooser();
        }
        if(v== bUpload){
    
            //on upload button Click
            if(selectedFilePath != null){
                dialog = ProgressDialog.show(MainActivity.this,"","Uploading File...",true);
    
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //creating new thread to handle Http Operations
                        uploadFile(selectedFilePath);
                    }
                }).start();
            }else{
                Toast.makeText(MainActivity.this,"Please choose a File First",Toast.LENGTH_SHORT).show();
            }
    
        }
    }
    
    private void showFileChooser() {
        Intent intent = new Intent();
        //sets the select file to all types of files
        intent.setType("*/*");
        //allows to select data and return it
        intent.setAction(Intent.ACTION_GET_CONTENT);
        //starts new activity to select file and return data
        startActivityForResult(Intent.createChooser(intent,"Choose File to Upload.."),PICK_FILE_REQUEST);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK){
            if(requestCode == PICK_FILE_REQUEST){
                if(data == null){
                    //no data present
                    return;
                }
    
    
                Uri selectedFileUri = data.getData();
                selectedFilePath = FilePath.getPath(this,selectedFileUri);
                Log.i(TAG,"Selected File Path:" + selectedFilePath);
    
                if(selectedFilePath != null && !selectedFilePath.equals("")){
                    tvFileName.setText(selectedFilePath);
                }else{
                    Toast.makeText(this,"Cannot upload file to server",Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
    
    //android upload file to server
    public int uploadFile(final String selectedFilePath){
    
        int serverResponseCode = 0;
    
        HttpURLConnection connection;
        DataOutputStream dataOutputStream;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
    
    
        int bytesRead,bytesAvailable,bufferSize;
        byte[] buffer;
        int maxBufferSize = 1 * 1024 * 1024;
        File selectedFile = new File(selectedFilePath);
    
    
        String[] parts = selectedFilePath.split("/");
        final String fileName = parts[parts.length-1];
    
        if (!selectedFile.isFile()){
            dialog.dismiss();
    
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tvFileName.setText("Source File Doesn't Exist: " + selectedFilePath);
                }
            });
            return 0;
        }else{
            try{
                FileInputStream fileInputStream = new FileInputStream(selectedFile);
                URL url = new URL(SERVER_URL);
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);//Allow Inputs
                connection.setDoOutput(true);//Allow Outputs
                connection.setUseCaches(false);//Don't use a cached Copy
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Connection", "Keep-Alive");
                connection.setRequestProperty("ENCTYPE", "multipart/form-data");
                connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                connection.setRequestProperty("uploaded_file",selectedFilePath);
    
                //creating new dataoutputstream
                dataOutputStream = new DataOutputStream(connection.getOutputStream());
    
                //writing bytes to data outputstream
                dataOutputStream.writeBytes(twoHyphens + boundary + lineEnd);
                dataOutputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                        + selectedFilePath + "\"" + lineEnd);
    
                dataOutputStream.writeBytes(lineEnd);
    
                //returns no. of bytes present in fileInputStream
                bytesAvailable = fileInputStream.available();
                //selecting the buffer size as minimum of available bytes or 1 MB
                bufferSize = Math.min(bytesAvailable,maxBufferSize);
                //setting the buffer as byte array of size of bufferSize
                buffer = new byte[bufferSize];
    
                //reads bytes from FileInputStream(from 0th index of buffer to buffersize)
                bytesRead = fileInputStream.read(buffer,0,bufferSize);
    
                //loop repeats till bytesRead = -1, i.e., no bytes are left to read
                while (bytesRead > 0){
                    //write the bytes read from inputstream
                    dataOutputStream.write(buffer,0,bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable,maxBufferSize);
                    bytesRead = fileInputStream.read(buffer,0,bufferSize);
                }
    
                dataOutputStream.writeBytes(lineEnd);
                dataOutputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    
                serverResponseCode = connection.getResponseCode();
                String serverResponseMessage = connection.getResponseMessage();
    
                Log.i(TAG, "Server Response is: " + serverResponseMessage + ": " + serverResponseCode);
    
                //response code of 200 indicates the server status OK
                if(serverResponseCode == 200){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvFileName.setText("File Upload completed.\n\n You can see the uploaded file here: \n\n" + "http://coderefer.com/extras/uploads/"+ fileName);
                        }
                    });
                }
    
                //closing the input and output streams
                fileInputStream.close();
                dataOutputStream.flush();
                dataOutputStream.close();
    
    
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainActivity.this,"File Not Found",Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (MalformedURLException e) {
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "URL error!", Toast.LENGTH_SHORT).show();
    
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "Cannot Read/Write File!", Toast.LENGTH_SHORT).show();
            }
            dialog.dismiss();
            return serverResponseCode;
        }
    
       }
       }
    

    add FilePath.cs

    public class FilePath {
    
    public static String getPath(final Context context, final Uri uri) {
    
        // check here to KITKAT or new version
        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    
        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
    
            // ExternalStorageProvider
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];
    
                if ("primary".equalsIgnoreCase(type)) {
                    return Environment.getExternalStorageDirectory() + "/"
                            + split[1];
                }
            }
            // DownloadsProvider
            else if (isDownloadsDocument(uri)) {
    
                final String id = DocumentsContract.getDocumentId(uri);
                final Uri contentUri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"),
                        Long.valueOf(id));
    
                return getDataColumn(context, contentUri, null, null);
            }
            // MediaProvider
            else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];
    
                Uri contentUri = null;
                if ("image".equals(type)) {
                    contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }
    
                final String selection = "_id=?";
                final String[] selectionArgs = new String[] { split[1] };
    
                return getDataColumn(context, contentUri, selection,
                        selectionArgs);
            }
        }
        // MediaStore (and general)
        else if ("content".equalsIgnoreCase(uri.getScheme())) {
    
            // Return the remote address
            if (isGooglePhotosUri(uri))
                return uri.getLastPathSegment();
    
            return getDataColumn(context, uri, null, null);
        }
        // File
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
    
        return null;
    }
    
    /**
     * Get the value of the data column for this Uri. This is useful for
     * MediaStore Uris, and other file-based ContentProviders.
     *
     * @param context
     *            The context.
     * @param uri
     *            The Uri to query.
     * @param selection
     *            (Optional) Filter used in the query.
     * @param selectionArgs
     *            (Optional) Selection arguments used in the query.
     * @return The value of the _data column, which is typically a file path.
     */
    public static String getDataColumn(Context context, Uri uri,
                                       String selection, String[] selectionArgs) {
    
        Cursor cursor = null;
        final String column = "_data";
        final String[] projection = { column };
    
        try {
            cursor = context.getContentResolver().query(uri, projection,
                    selection, selectionArgs, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int index = cursor.getColumnIndexOrThrow(column);
                return cursor.getString(index);
            }
        } finally {
            if (cursor != null)
                cursor.close();
        }
        return null;
    }
    
    /**
     * @param uri
     *            The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri
                .getAuthority());
    }
    
    /**
     * @param uri
     *            The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri
                .getAuthority());
    }
    
    /**
     * @param uri
     *            The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri
                .getAuthority());
    }
    
    /**
     * @param uri
     *            The Uri to check.
     * @return Whether the Uri authority is Google Photos.
     */
    public static boolean isGooglePhotosUri(Uri uri) {
        return "com.google.android.apps.photos.content".equals(uri
                .getAuthority());
    }
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorAccent">
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="18sp"
        android:id="@+id/tvHeading"
        android:text="Touch the icon below to upload file to server"
        android:textColor="#fff"
        android:textStyle="bold"/>
    <ImageView
        android:id="@+id/ivAttachment"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@android:drawable/ic_menu_camera"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>
    
    <TextView
        android:id="@+id/tv_file_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#fff"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:layout_below="@+id/ivAttachment"
        android:layout_centerHorizontal="true"/>
    
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/b_upload"
        android:text="Upload"
        android:textStyle="bold"
        android:textSize="20sp"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:textColor="#fff"
        android:background="#039be5"/>
    
      </RelativeLayout>
    

    php file a.php

    <?php
     if (isset($_FILES["uploaded_file"]["name"])) {
    
    $name = $_FILES["uploaded_file"]["name"];
    $tmp_name = $_FILES['uploaded_file']['tmp_name'];
    $error = $_FILES['uploaded_file']['error'];
    
    if (!empty($name)) {
        $location = './fileload/';
    
     if ( ! is_dir($location)) {
     mkdir($location);
     }
    
        if  (move_uploaded_file($tmp_name, $location.$name)){
            echo 'Uploaded';
        }
    
    } else {
        echo 'please choose a file';
    }
    }
     ?>
    

    add read/write permission to the fileload folder

    tested with fiddler with following code

      User-Agent: Fiddler
      Content-Type: multipart/form-data;boundary=*****
      uploaded_file: C:\Desktop\oldScreen.png
      Content-Disposition: form-data; 
      name="uploaded_file";filename="C:\\Desktop\oldScreen.png"
      Content-Length: 0
    
    0 讨论(0)
  • Try this: it 100% working copy.

    Upload function: (Parameter to a function is the imagePath)

    public void uploadFile(String sourceFileUri) {
    
    
              String fileName = imagePath;
              HttpURLConnection conn = null;
              DataOutputStream dos = null;  
              String lineEnd = "\r\n";
              String twoHyphens = "--";
              String boundary = "*****";
              int bytesRead, bytesAvailable, bufferSize;
              byte[] buffer;
              int maxBufferSize = 1 * 1024 * 1024; 
              File sourceFile = new File(imagePath); 
    
              if (!sourceFile.isFile()) {
    
                    if (dialog != null && dialog.isShowing()) {
                        dialog.dismiss();
                    }
    
                Log.e("uploadFile", "Source File not exist :"+imagePath);
    
    
              }
              else
              {
                try { 
    
                   // open a URL connection to the Servlet
                    FileInputStream fileInputStream = new FileInputStream(sourceFile);
                    URL url = new URL(Upload_Image_URL);
    
                    // Open a HTTP  connection to  the URL
                    conn = (HttpURLConnection) url.openConnection(); 
                    conn.setDoInput(true); // Allow Inputs
                    conn.setDoOutput(true); // Allow Outputs
                    conn.setUseCaches(false); // Don't use a Cached Copy
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                    conn.setRequestProperty("uploaded_file", fileName); 
    
                    dos = new DataOutputStream(conn.getOutputStream());
    
                    dos.writeBytes(twoHyphens + boundary + lineEnd); 
                    dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                                        + fileName + "\"" + lineEnd);
    
                    dos.writeBytes(lineEnd);
    
                    // create a buffer of  maximum size
                    bytesAvailable = fileInputStream.available(); 
    
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    buffer = new byte[bufferSize];
    
                    // read file and write it into form...
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);  
    
                    while (bytesRead > 0) {
    
                      dos.write(buffer, 0, bufferSize);
                      bytesAvailable = fileInputStream.available();
                      bufferSize = Math.min(bytesAvailable, maxBufferSize);
                      bytesRead = fileInputStream.read(buffer, 0, bufferSize);   
    
                     }
    
                    // send multipart form data necesssary after file data...
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
    
                    // Responses from the server (code and message)
                    serverResponseCode = conn.getResponseCode();
                    String serverResponseMessage = conn.getResponseMessage();
    
                    Log.i("uploadFile", "HTTP Response is : " 
                      + serverResponseMessage + ": " + serverResponseCode);
    
    
                    //close the streams //
                    fileInputStream.close();
                    dos.flush();
                    dos.close();
    
               } catch (MalformedURLException ex) {
    
                        if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                        }
                   ex.printStackTrace();
    
                   Log.e("Upload file to server", "error: " + ex.getMessage(), ex);  
               } catch (Exception e) {
    
                   if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
                   }
                   e.printStackTrace();
    
    
               }
    
                if (dialog != null && dialog.isShowing()) {
                            dialog.dismiss();
    
                }  
    
    
                   } // End else block 
                 } 
    

    Php Service:

      $file_path = "images/";
      $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
    
      if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
      echo "success";
      } else{ echo "fail";}
    

    Must create a folder named "images" in the server so $file_path is valid

    0 讨论(0)
  • 2020-11-27 03:19

    Php file

     <?php
    
    
     if (is_uploaded_file($_FILES['bill']['tmp_name'])) {
    $uploads_dir = 'Uploads/staff_documents/';
                            $tmp_name = $_FILES['bill']['tmp_name'];
                            $pic_name = $_FILES['bill']['name'];
                            echo $pic_name;
                            move_uploaded_file($tmp_name, $uploads_dir.$pic_name);
                            }
               else{
                   echo "File not uploaded successfully.";
           }
    
    ?>
    

    It supports All file To upload docs from the android device you can make use of https://github.com/gotev/android-upload-service Because it gives notification support as well

    Android Java Part

     new MultipartUploadRequest(getApplicationContext(), uploadId, "Your server 
     url")
                            .addFileToUpload(filePathURI + "","bill") 
                             //Adding file
                            .setNotificationConfig(new UploadNotificationConfig())
                            .setMaxRetries(2)
                            .startUpload();
    
    0 讨论(0)
提交回复
热议问题