Ответы пользователя по тегу PHP
  • Не получается отправить файл на сервер?

    @Gfd Автор вопроса
    Если кому то будет интересно, то вот так я решил
    Android:
    public void onClickSendImage(View view) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 1);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
            super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
    
            if (resultCode == RESULT_OK) {
                Uri photoUri = imageReturnedIntent.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(photoUri, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                mFilePath = cursor.getString(columnIndex);
                cursor.close();
    
                file = new File(mFilePath);
                image_name = getFileName(mFilePath);
    
                new Encode_image().execute();
            }
        }
    
        private String getFileName(String filePath) {
            char[] chars = filePath.toCharArray();
    
            int index = 0;
    
            for (int i = chars.length-1; i > 0; i--) {
                if (chars[i] == '\\' || chars[i] == '/') {
                    index = i;
                    break;
                }
            }
    
            String result = "";
    
            for (int i = index+1; i < chars.length; i++) {
                result = result + String.valueOf(chars[i]);
            }
    
            return result;
        }
    
        private class Encode_image extends AsyncTask<Void, Void, Void> {
            @Override
            protected Void doInBackground(Void... voids) {
    
                bitmap = BitmapFactory.decodeFile(mFilePath);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
                bitmap.recycle();
    
                byte[] array = stream.toByteArray();
                encoded_string = Base64.encodeToString(array, 0);
                return null;
            }
    
            @Override
            protected void onPostExecute(Void aVoid) {
                makeRequest();
            }
        }
    
        private void makeRequest() {
            RequestQueue requestQueue = Volley.newRequestQueue(this);
            StringRequest request = new StringRequest(Request.Method.POST, "http://potatosing.16mb.com/index.php",
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {
    
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
    
                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    HashMap<String,String> map = new HashMap<>();
                    map.put("encoded_string",encoded_string);
                    map.put("image_name",image_name);
    
                    return map;
                }
            };
            requestQueue.add(request);
        }


    PHP:
    <?php 
        header('Content-type : bitmap; charset=utf-8');
     
        if(isset($_POST["encoded_string"])){
     	
            $encoded_string = $_POST["encoded_string"];
            $image_name = $_POST["image_name"];
    
            $decoded_string = base64_decode($encoded_string);
    
            $path = 'images/'.$image_name;
    
            $file = fopen($path, 'wb');
    
            $is_written = fwrite($file, $decoded_string);
            fclose($file);
        }
    ?>
    Ответ написан
    Комментировать