PHP Ajax 图像上传错误
PHP Ajax Image Upload bug
我现在尝试让我的Ajax图像上传工作。但遗憾的是它不起作用,我找不到原因。
<script>
$(document).ready(function() {
$("#uploadBTN").click(function(event) {
event.preventDefault();
var tmp_form = $("#fileinfo")[0];
var fd = new FormData(tmp_form);
$.ajax({
url: './ajax/image_post_upload.php',
type: 'POST',
data: fd,
async: true,
success:function(data){
$('#output').html(data);
},
cache: false,
contentType: false,
processData: false
});
});
});
</script>
<form id="fileinfo">
<input id="fileinfo" name="userImage" type="file" class="file" />
<div id="uploadBTN">SUBMIT</div>
</form>
<div id=output>LOG HERE</div>
image_post_upload.php
<?php
if(isset($_FILES['file'])){
$filename = $_FILES['file']['name'];
if(isset($_FILES['file']['name']) && !empty($filename)){
$target_dir = "./UPLOADS/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["file"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
$root = getcwd();
if (move_uploaded_file($_FILES["file"]["tmp_name"], $root.$target_file)) {
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
}else{
echo 'please choose a file';
}
}else{
echo 'not set';
}
?>
此刻它每次都说没有设置?我的脚本中出了什么问题?
文件输入的 HTML name
属性与 PHP $_FILES['file']
不匹配,这就是if(isset($_FILES['file']))
返回 false 的原因。
您可以通过更改 HTML 以匹配您的 PHP 来解决此问题:
<input id="fileinfo" name="file" type="file" class="file" />
希望对您有所帮助!
您的文件输入名称是userImage,并且您正在以$_FILES['file']
访问它,
因此,请使用以下两个代码中的任何一个代码。
HTML : <input id="fileinfo" name="userImage" type="file" class="file" />
菲律宾比索 : $_FILES['userImage']
网页 : <input id="fileinfo" name="file" type="file" class="file" />
菲律宾比索 : $_FILES['file']
相关文章:
- 从安卓设备将图像上传到服务器时,文件扩展名错误
- 未捕获的类型错误: 无法读取未定义的属性“图像”
- 在Firefox中导出高图表时出现黑色图像错误
- 错误:$injector:modulerr加载图像时模块处于角度
- 错误时用图像替换视频
- jQuery:获取图像加载错误的详细信息
- 在照片查看器(Javascript和jQuery)中异步加载图像时可能出现错误
- 尝试getUrl图像时,Firefox WebExtension出现安全错误
- cordova图像上传出现文件传输错误
- JS预加载图像错误
- JQuery 图像错误处理
- 图像滑块的错误
- 铯:使用自己的OpenStreetMap服务器.:“无法获取图像磁贴”错误
- lightgallery type未加载错误图像
- 火狐 SDK 错误“图像包含错误,无法显示”
- JavaScript 代码错误:图像不显示
- 使用带有错误图像路径的 jQuery 加载打开的页面
- IE 11错误-图像在标签内的形式-需要保存鼠标事件
- 图像交替功能交替错误图像
- 如何为页面上的所有图像设置错误图像