Answer the question
In order to leave comments, you need to log in
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();
}
}
<?php
$uploaddir = 'files/';
$uploadfile = $uploaddir.basename($_FILES['image']['name']);
?>
$uploaddir = 'files/';
$uploadfile = $uploaddir.basename($_FILES['image']['name']);
move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile);
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question