G
G
Gfd2016-12-03 18:03:14
PHP
Gfd, 2016-12-03 18:03:14

Can't send file to server?

android:

public void onClickSend(View view) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 1);
        }
    
            private class ConnectToServer extends AsyncTask<Void, Void, Integer> {
    
            HttpURLConnection conn;
            Integer res;
    
            protected Integer doInBackground(Void... params) {
                try {
                    File file = new File(mFilePath);
    
                    HttpRequest request = HttpRequest.post("http://potatosing.16mb.com");
                    request.part("image", "image.jpg", file);
    
                    URL url = new URL(request.body());
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(1000000);
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("User-Agent", "Mozilla/5.0");
                    conn.connect();
    
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return res;
            }
        }
    
        @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();
    
                new ConnectToServer().execute();
            }
        }

And the server itself in php (index.php):
<?php 
        $uploaddir = 'files/';
        $uploadfile = $uploaddir.basename($_FILES['image']['name']);
    ?>

It seems that no android does not give an error, but for some reason the file is not saved.
PS: I'm a newbie
Update:
I added the move_uploaded_file method.
Here's what happened now:
$uploaddir = 'files/';
    $uploadfile = $uploaddir.basename($_FILES['image']['name']);
    move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile);

Still nothing works

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Polin, 2016-12-03
@Gfd

I don't rummage in andryusha, but you send a Post file with a request, but you don't accept the request on the server. Moreover, where is the file size check? File type? Moreover, it would not be bad to send at least something in response to the client. And where is the file moving from the temporary directory?
UPD
This code is 100% working, although it leaves much to be desired. Tested on php 5.6. The problem can only arise with the download directory. In this code, a directory with this path was used $uploaddir = './files/';
. Try both options for specifying the path to save.

$uploaddir = 'files/';
$file = $uploaddir . basename($_FILES['image']['name']); 
$ext = substr($_FILES['image']['name'],strpos($_FILES['image']['name'],'.'),strlen($_FILES['image']['name'])-1); 
$filetypes = array('.jpg','.gif','.bmp','.png','.JPG','.BMP','.GIF','.PNG','.jpeg','.JPEG');
$name=$_FILES['image']['name'];
$getMime = explode('.', $name);
$mime = end($getMime);
$randomName = substr_replace(sha1(microtime(true)), '', 12);
$filename=$randomName.'.'.$mime;
move_uploaded_file($_FILES['image']['tmp_name'],$uploaddir .$filename.'.'.$mime);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question