duanchai0028 2014-03-08 13:48
浏览 45
已采纳

如何更改上传文件/ Android + PHP的名称

I'm trying to figure out how can i change the name of a file i'm about to upload.

I really prefer to changed it's name before i upload it to the server

This is the upload method at client(Android) side -

   private void doFileUpload(){

    File file1 = new File(pathofFile);


    String urlString = "http://12.8.1.10/uploads/upload_media_test.php";



    try
    {
         HttpClient client = new DefaultHttpClient();
         HttpPost post = new HttpPost(urlString);
         FileBody bin1 = new FileBody(file1);
         FileBody bin2 = new FileBody(file2);
         MultipartEntity reqEntity = new MultipartEntity();
         reqEntity.addPart("uploadedfile1", bin1);
         reqEntity.addPart("user", new StringBody("User"));
         post.setEntity(reqEntity);
         HttpResponse response = client.execute(post);
         resEntity = response.getEntity();
         final String response_str = EntityUtils.toString(resEntity);
         if (resEntity != null) {
             Log.i("RESPONSE",response_str);
             runOnUiThread(new Runnable(){
                    public void run() {
                         try {

                            Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                       }
                });
         }
    }
    catch (Exception ex){
         Log.e("Debug", "error: " + ex.getMessage(), ex);
    }
  }

If it's possible to do it at the client side - please try to be specific as you can about how to do so.

If it's not possible, and the only option is to do that at the server side, so i adding the php code - so you could explain me what i need to change there, thanks

Php code -

    $target_path1 = "uploads/";

$target_path1 = $target_path1 . basename( $_FILES['uploadedfile1']['name']);
if(move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1)) {
    echo "The first file ".  basename( $_FILES['uploadedfile1']['name']).
    " has been uploaded.";
} else{
    echo "There was an error uploading the file, please try again!";
    echo "filename: " .  basename( $_FILES['uploadedfile1']['name']);
    echo "target_path: " .$target_path1;
}
  • 写回答

1条回答 默认 最新

  • douxu3315 2014-03-08 14:09
    关注

    In the target path you can change the name here,

     $fileName = $_FILES["uploadedfile1"]["name"];
     $ext = "." . end(explode(".", $fileName));
     $target_path1 = "uploads/";
    
     $target_path1 = $target_path1 . $name_of_file_here . $ext;
     if(move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1)) {
      echo "The first file ". basename( $_FILES['uploadedfile1']['name'])." has been uploaded.";
    //rest of code here....
    

    Also as a side note, if you want your file name to come from your android, you will have to send additional post data with the name you wish and set $name_of_file_here to that variable. The $target_path1 should read similarly to "uploads/myPic.jpg"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题