繁体   English   中英

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

[英]How to change the name of uploaded file /Android + PHP

我试图弄清楚如何更改要上传的文件的名称。

我真的更喜欢在将其上传到服务器之前更改其名称

这是客户端(Android)端的上传方法-

   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);
    }
  }

如果有可能在客户端执行此操作,请尝试具体说明操作方法。

如果不可能,并且唯一的选择是在服务器端执行此操作,那么我添加了php代码-这样您就可以解释一下我需要在此处进行更改的内容,谢谢

邮递区号-

    $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;
}

在目标路径中,您可以在此处更改名称,

 $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....

另外请注意,如果您希望文件名来自android,则必须发送其他具有所需名称的post数据,并将$name_of_file_here设置为该变量。 $target_path1应该类似于“ uploads / myPic.jpg

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM