gpt4 book ai didi

php - 使用 OkHttp 和 PHP 将文件从 android 上传到服务器

转载 作者:行者123 更新时间:2023-11-30 01:51:00 25 4
gpt4 key购买 nike

我制作了 Android uploader 和 php 脚本。但我无法从 android 本地文件夹上传 csv 文件。

PHP 脚本可以单独工作。

Error Undefined index:upfile in /example/html/php/ uploadfile.php online 9

不要选择文件

public class MainActivity extends Activity {

TextView outputText;
Button sendData;
EditText edtUser, edtPass;
final String URL = "http://example/php/uploadfile.php";


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

outputText = (TextView) findViewById(R.id.textView1);
outputText.setMovementMethod(new ScrollingMovementMethod());
sendData = (Button) findViewById(R.id.button1);
edtUser = (EditText) findViewById(R.id.editText1);
edtPass = (EditText) findViewById(R.id.editText2);

String Text = "Tue Oct 06 18:56:24 JST 2015,Tue Oct 06 18:57:12 JST 2015,35.4708118,139.6251623\n";

try {
OutputStream out = openFileOutput("GPS_log2.csv", MODE_APPEND);
PrintWriter writer =
new PrintWriter(new OutputStreamWriter(out, "UTF-8"));
writer.append(Text);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}

sendData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

OkHttpHandler handler = new OkHttpHandler();

String result = null;

try {
result = handler.execute(URL).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
outputText.append(result + "\n");
}
});
}
}


public class OkHttpHandler extends AsyncTask<String, Void, String> {

public static final MediaType MEDIA_TYPE_CSV
= MediaType.parse("text/csv; charset=utf-8");

OkHttpClient client = new OkHttpClient();

public OkHttpHandler(){

}

@Override
protected String doInBackground(String........params){

String filename = "GPS_log2.csv";
Log.d("doInBackground", "start");

Request request = new Request.Builder()
.url("http://160.16.84.183/php/uploadfile.php")
.post(RequestBody.create(MEDIA_TYPE_CSV,filename))
.build();

try{
Response response = client.newCall(request).execute();
if(!response.isSuccessful())
throw new IOException("Unexpected code " + response.toString());
return response.body().string();

}catch(Exception e){
e.printStackTrace();
Log.d("Response ",e.getMessage());

}
return null;

}

if (is_uploaded_file($_FILES["upfile"]["tmp_name"])) {
if (move_uploaded_file($_FILES["upfile"]["tmp_name"], "/var/www/html/php/" . $_FILES["upfile"]["name"])) {
chmod("/var/www/html/php/" . $_FILES["upfile"]["name"], 0644);
echo $_FILES["upfile"]["name"] . "file uploaded";
} else {
echo "cannot file upload";
}
} else {
echo "do not choice file";
}

最佳答案

aine , you are trying to write file at /var/www/html/php/. it is not best practice to write files this location . Move your loaction of script to /home and change the path from etc/apache/siteenabled . For more details 
http://askubuntu.com/questions/379412/how-to-redirect-apache-var-www-to-the-home-directory-and-access-all-the-directo
For your PHP code must be as follows
public static function processRequest()
{
case 'post':
if(move_uploaded_file($_FILES["uploaded_file"]["tmp_name"] , "Images/" . $_FILES["uploaded_file"]["name"] )){
echo "move_uploaded_file SUCCESS ";
......................................
}
......................................
protected function executePost ($ch)
{
$tmpfile = $_FILES['image1']['tmp_name'];
$filename = basename($_FILES['image1']['name']);

$data = array(
'uploaded_file' => '@' . $tmpfile . ';filename='.$filename,
);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//no need httpheaders
$this->doExecute($ch);
}

关于php - 使用 OkHttp 和 PHP 将文件从 android 上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33052668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com