![](/img/trans.png)
[英]How to send javascript variable to php to change the file name of the uploaded file?
[英]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.