2 回答

TA貢獻1860條經驗 獲得超9個贊
您的 PHP 代碼和 HTML 中存在多個邏輯錯誤。
檢查表單提交時,您必須檢查“上傳”(提交按鈕的名稱)是否在 $_POST 中。
文件上傳輸入應位于 <form> 標記內。
為文件上傳字段設置一個名稱,我將其設置為“userImageUpload”,這樣您就可以從 PHP 中的 $_FILES 訪問它。
這是最終的代碼:
<?php
if (!empty($_POST["upload"])) {
if (is_uploaded_file($_FILES['userImageUpload']['tmp_name'])) {
$targetPath = "uploads/" . $_FILES['userImageUpload']['name'];
if (move_uploaded_file($_FILES['userImageUpload']['tmp_name'], $targetPath)) {
$uploadedImagePath = $targetPath;
}
}
}
?>
<img id="userImage" />
<script>
var loadFile = function(event) {
var output = document.getElementById('userImage');
output.src = URL.createObjectURL(event.target.files[0]);
output.onload = function() {
URL.revokeObjectURL(output.src)
} // free memory
};
</script>
<form id="uploadForm" action="" method="post" enctype="multipart/form-data">
<input type="file" accept="image/*" onchange="loadFile(event)" name="userImageUpload">
<input type="submit" name="upload" value="Submit" class="btnSubmit">
</form>
注意:請確?!吧蟼鳌蔽募A已存在并且權限也正確。

TA貢獻1812條經驗 獲得超5個贊
您已將輸入放在表單之外,但它應該在其中:
<form id="uploadForm" action="" method="post" enctype="multipart/form-data">
<input type="file" accept="image/*" onchange="loadFile(event)">
<input type="submit" name="upload" value="Submit" class="btnSubmit">
</form>
- 2 回答
- 0 關注
- 131 瀏覽
添加回答
舉報