2014-08-31 68 views
0

美好的一天。 我做了一個移動應用程序,拍照並將其發送到網頁.php以顯示給用戶。 我已成功發送圖片並將其保存在服務器網站的特定目錄中,並以新名稱19.jpg 問題是我正試圖保存原始文件名。 但如果文件名使用了阿拉伯語語言如 「مرحبا」 它看起來像 「0.1)」 我試圖用將圖片從android手機上傳到網頁.php

的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 
     } 
+0

請加上Android應用程序的您發送方法,所以我也許可以幫你解決問題 – 2014-08-31 18:47:09

+0

我做了最簡單的代碼展示。 – 2014-08-31 19:01:41

回答

0

試試這個,也許是解決這個問題

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

而在PHP文件使用這種

echo urldecode($original_file_name); 
+0

現在我知道了,我不知道它是什麼。%2Fstorage%2Fsdcard0%D8%AE%D8%B1%D8%B1%D8%A8.jpg – 2014-08-31 19:21:08

+0

將urldecode添加到您的php文件 – 2014-08-31 19:34:10

+0

am keep getting an空字符串:( – 2014-08-31 19:35:12