新人求助,关于android上传图片,php作为服务器端接收图片问题

时间:2022-08-28 23:41:54
问题描述:android端向php端发送发送图片,php接收图片,将图片路径写入数据库,测试的两台服务器,其中一台正常,另外一台无法接收图片,现在不知道是不是服务器的问题,请各位大神帮忙看看,不胜感谢!!!
php端:
$sub_path = './' . time() . basename($_FILES['uploadedfile']['name']);
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $sub_path)) {
echo json_encode('success');
} else {
echo json_encode('fail');
}

android端:
class UpLoadImage extends AsyncTask<Void,Void,String>{
        private ProgressDialog dialog = null;
        HttpURLConnection connection = null;
        DataOutputStream outputStream = null;
        DataInputStream inputStream = null;
        String lineEnd = "\r\n";
        String twoHyphens = "--";
        String boundary = "*****";
        @Override
        protected void onPreExecute() {
            dialog=ProgressDialog.show(WodecallDetailActivity.this,"提示","图片上传中。。。",true);
        }

        @Override
        protected String doInBackground(Void... params) {
            long totalSize = uploadFile.length(); // Get size of file, bytes

            long length = 0;
            int progress;
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 2560 * 10240;// 25600KB
            try {
                FileInputStream fileInputStream = new FileInputStream(new File(
                        photo_path));
                URL url = new URL("http://119.4.210.230:8000/index.php/home/Interface/upload_test/call_id/"+intent.getStringExtra("id"));
                connection = (HttpURLConnection) url.openConnection();
                // Set size of every block for post
                connection.setChunkedStreamingMode(2560 * 10240);// 25600KB
                // Allow Inputs & Outputs
                connection.setDoInput(true);
                connection.setDoOutput(true);
                connection.setUseCaches(false);
                // Enable POST method
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Connection", "Keep-Alive");
                connection.setRequestProperty("Charset", "UTF-8");
                connection.setRequestProperty("Content-Type",
                        "multipart/form-data;boundary=" + boundary);
                outputStream = new DataOutputStream(
                        connection.getOutputStream());
                outputStream.writeBytes(twoHyphens + boundary + lineEnd);
                outputStream
                        .writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                                + photo_path + "\"" + lineEnd);
                outputStream.writeBytes(lineEnd);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];
                // Read file
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                while (bytesRead > 0) {
                    outputStream.write(buffer, 0, bufferSize);
                    length += bufferSize;
                    progress = (int) ((length * 100) / totalSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                }
                outputStream.writeBytes(lineEnd);
                outputStream.writeBytes(twoHyphens + boundary + twoHyphens
                        + lineEnd);
                // Responses from the server (code and message)
                int serverResponseCode = connection.getResponseCode();
                Log.v("serverResponseCode", "serverResponseCode======" + serverResponseCode);
                Log.v("serverResponseCode", "serverResponseCode======" + serverResponseCode);
                Log.v("serverResponseCode", "serverResponseCode======" + serverResponseCode);
                Log.v("serverResponseCode", "serverResponseCode======" + serverResponseCode);
                Log.v("serverResponseCode", "serverResponseCode======" + serverResponseCode);
                String serverResponseMessage = connection.getResponseMessage();
                InputStream is = connection.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader bfr = new BufferedReader(isr);
                String result = bfr.readLine();
                fileInputStream.close();
                outputStream.flush();
                outputStream.close();
                return result;
            } catch (Exception ex) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            try {
                dialog.dismiss();
            }catch (Exception e){

            }
            shangchuan.setVisibility(View.GONE);
            Log.v("resut", "result:" + s);
            Myutils.myToast(getApplicationContext(),"上传图片成功");
        }
    }

1 个解决方案

#1


将$_FILES打印到一个txt文件里面看一下有没有数据就知道了(file_put_contents,var_export)
一台服务器可以,一台不行,就有可能是路径的问题了,移动失败

#1


将$_FILES打印到一个txt文件里面看一下有没有数据就知道了(file_put_contents,var_export)
一台服务器可以,一台不行,就有可能是路径的问题了,移动失败