這個問題被問了很多次,但我找不到正確的答案。
我正在嘗試使用 javascript、ajax 和 php 上傳檔案。到目前為止有效。但是,我想在上傳時和在 javascript 中重命名檔案,以便我可以從那里確定檔案的名稱。
我在javascript中通過ajax上傳檔案的功能
async function uploadFile() {
let formData = new FormData();
formData.append("file", fImage.files[0]);
await fetch('/upload.php', {
method: "POST",
body: formData
});
alert('The file has been uploaded successfully.');
}
我在html中的輸入欄位
<input class="form-control" type="file" id="fImage">
我的上傳.php
<?php
/* Get the name of the uploaded file */
$filename = $_FILES['file']['name'];
/* Choose where to save the uploaded file */
$location = "upload/".$filename;
/* Save the uploaded file to the local filesystem */
if ( move_uploaded_file($_FILES['file']['tmp_name'], $location) ) {
echo 'Success';
} else {
echo 'Failure';
}
?>
目的是通過 javascript 確定檔案名,例如通過變數
uj5u.com熱心網友回復:
您可以使用的第三個引數append來指定檔案名(MDN)
formData.append("file", fImage.files[0], new_name);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/503642.html
標籤:javascript php 阿贾克斯 文件 上传
下一篇:如何在陣列php中過濾零?
