我正在try 使用表格数据上传图像.但表单域服务器所需的名称无法识别.
Warning: Undefined array key "photo"
以下是我的代码:
index.html个
html>
<head>
<script type = "text/javascript" src="index.js"></script>
</head>
<body>
<button onclick="uploadImage()">upload an Image</button>
</body>
</html>
index.js
async function uploadImage(){
const data = new FormData()
data.append('photo',{
name: 'timg',
type: 'image/jpeg',
uri: 'C:\Users\mea\Downloads\testimg.jpeg'
})
return await fetch('upload_image.php',{
method: 'POST',
body: data,
headers: {
'Content-Type': 'multipart/form-data'
}
})
}
upload_image.php个
<?php
header('Access-Control-Allow-Origin: *');
$target_dir = "../../images";
$target_file = $target_dir .'/'.basename($_FILES['photo']['name']);
$name = $_FILES['photo']['name'];
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target_file)) {
echo json_encode (
array('message' => 'Image uploaded Successfully'));}
else{
echo json_encode (
array('message' => 'Upload failed!'));
}
?>
我try 了所有可用的解决方案,但仍找不到原因.