将图片从Android手机上传到网页.php

时间:2014-08-31 18:37:50

标签: php android encoding

美好的一天。 我制作了一个移动应用程序,拍摄照片并将其发送到网页.php以显示给用户。 我已成功发送图片并将其保存在服务器站点的特定目录中,名称为19.jpg 问题是我想保存原始文件名。 但如果文件名是阿拉伯语,如“مرحبا”,它看起来像“0.1” 我试着用

的iconv(cp1256,UTF-8,$ origonalname)

但没有用

这是我在服务器上保存文件的代码

<?php 
$file_path=$file_path.basename($_FILES['uploaded_file']['name']);
$original_file_name=iconv("UTF-8",$_FILES['uploaded_file']['name']);  
echo $original_file_name;
   //*******************************

   if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'],"uploads/19.jpg")){
       echo "success";
   }else{
       echo "fail";
   }
        ?>

我不知道我从手机或服务器网站上发帖时错过了哪里。 下面是android的上传功能

upLoadServerUri = "http://www.myserver.com/UploadToServer.php";
 public int uploadFile(String sourceFileUri) {
        note=t1.getText()+"";
          sourceFileUri=(Environment.getExternalStorageDirectory()+
                  note+".jpg");
          String fileName = sourceFileUri;

          HttpURLConnection conn = null;
          DataOutputStream dos = null;  
          String lineEnd = "\r\n";
          String twoHyphens = "--";
          String boundary = "*****";
          int bytesRead, bytesAvailable, bufferSize;
          byte[] buffer;
          int maxBufferSize = 1 * 1024 * 1024; 
          File sourceFile = new File(Environment.getExternalStorageDirectory(),
                  note+".jpg"); 

          if (!sourceFile.isFile()) {

               dialog.dismiss(); 

               Log.e("uploadFile", "Source File not exist :"+imagepath);

               runOnUiThread(new Runnable() {
                   public void run() {
                       messageText.setText("Source File not exist :"+ imagepath);
                   }
               }); 

               return 0;

          }
          else
          {
               try { 

                     // open a URL connection to the Servlet
                   FileInputStream fileInputStream = new FileInputStream(sourceFile);
                   URL url = new URL(upLoadServerUri);

                   // Open a HTTP  connection to  the URL
                   conn = (HttpURLConnection) url.openConnection(); 
                   conn.setDoInput(true); // Allow Inputs
                   conn.setDoOutput(true); // Allow Outputs
                   conn.setUseCaches(false); // Don't use a Cached Copy
                   conn.setRequestMethod("POST");
                   conn.setRequestProperty("Connection", "Keep-Alive");
                   conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                   conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                   conn.setRequestProperty("uploaded_file", fileName); 

                   dos = new DataOutputStream(conn.getOutputStream());

                   dos.writeBytes(twoHyphens + boundary + lineEnd); 
                   dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""
                                             + fileName + "\"" + lineEnd);

                   dos.writeBytes(lineEnd);

                   // create a buffer of  maximum size
                   bytesAvailable = fileInputStream.available(); 

                   bufferSize = Math.min(bytesAvailable, maxBufferSize);
                   buffer = new byte[bufferSize];

                   // read file and write it into form...
                   bytesRead = fileInputStream.read(buffer, 0, bufferSize);  

                   while (bytesRead > 0) {

                     dos.write(buffer, 0, bufferSize);
                     bytesAvailable = fileInputStream.available();
                     bufferSize = Math.min(bytesAvailable, maxBufferSize);
                     bytesRead = fileInputStream.read(buffer, 0, bufferSize);   

                    }

                   // send multipart form data necesssary after file data...
                   dos.writeBytes(lineEnd);
                   dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                   // Responses from the server (code and message)
                   serverResponseCode = conn.getResponseCode();
                   String serverResponseMessage = conn.getResponseMessage();

                   Log.i("uploadFile", "HTTP Response is : " 
                           + serverResponseMessage + ": " + serverResponseCode);

                   if(serverResponseCode == 200){

                       runOnUiThread(new Runnable() {
                            public void run() {
                                String msg = "يمكنك ارسال صورة اخرى";
                                messageText.setText(msg);
                                Toast.makeText(MainActivity.this, "تم التحميل بنجاح نشكركم لتعاونكم", Toast.LENGTH_SHORT).show();
                            }
                        });                
                   }    

                   //close the streams //
                   fileInputStream.close();
                   dos.flush();
                   dos.close();

              } catch (MalformedURLException ex) {

                  dialog.dismiss();  
                  ex.printStackTrace();

                  runOnUiThread(new Runnable() {
                      public void run() {
                          messageText.setText("MalformedURLException Exception : check script url.");
                          Toast.makeText(MainActivity.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
                      }
                  });

                  Log.e("Upload file to server", "error: " + ex.getMessage(), ex);  
              } catch (Exception e) {

                  dialog.dismiss();  
                  e.printStackTrace();

                  runOnUiThread(new Runnable() {
                      public void run() {
                          messageText.setText("Got Exception : see logcat ");
                          Toast.makeText(MainActivity.this, "Got Exception : see logcat ", Toast.LENGTH_SHORT).show();
                      }
                  });
                  Log.e("Upload file to server Exception", "Exception : "  + e.getMessage(), e);  
              }
              dialog.dismiss();       
              return serverResponseCode; 

           } // End else block 
         }

1 个答案:

答案 0 :(得分:0)

试试这个,也许解决问题

//String fileName = sourceFileUri;
String fileName = URLEncoder.encode((Environment.getExternalStorageDirectory()+note+".jpg"),"UTF-8");

在php文件中使用此

echo urldecode($original_file_name);