jQuery의 ajax()
기능 과 함께 PHP를 사용하여 서버에 파일을 업로드하려고했습니다 . 아래는 내가 시도한 코드입니다. jQuery없이 동일한 페이지에 PHP 코드를 작성했을 때 모든 것이 잘 작동했기 때문에 파일 업로드가 작동하고 있다는 것은 의심의 여지가 없습니다.
HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(function() {
$("form").submit(function(e){
e.preventDefault();
var fd = new FormData();
fd.append("files", $("#fileinput").prop("files"));
$.ajax({
url: "imgupload_.php",
type:"POST",
processData: false,
contentType: false,
data: fd,
success: function(result){
alert(result);
}
});
});
});
</script>
<form method="POST" action="#" enctype="multipart/form-data">
<input type='file' name='files' id="fileinput"/>
<input type='submit' value='Submit' name='submit'/>
</form>
imgupload_.php
if(isset($_POST["submit"])) {
$target_dir = "images/";
$target_file = $target_dir . basename($_FILES["files"]["name"]);
if (move_uploaded_file($_FILES["files"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["files"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
다른 정보를 원하시면 아래에 의견을 남겨주세요.
확인 if(isset($_POST["submit"]))
중이지만 설정되지 않았습니다.
이 대신 :
var fd = new FormData();
이를 사용하여 파일이 아닌 입력 값 (예 : 제출 버튼)을 가져옵니다.
var fd = new FormData(this);
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다