我正在使用AJAX窗体通过ajax在我的网站上载个人资料图片,但事情是我无法做到。我做了一个PHP页面,表单将通过ajax发布。看一看:PHP无法正常工作
<?php
$file = $_FILES["file"];
$filename = $_FILES["file"]["name"];
$tempdir = $_FILES["file"]["tmp_name"];
$error = $_FILES["file"]["error"];
$type = $_FILES["file"]["type"];
$size = $_FILES["file"]["size"];
$maxsize = 524288;
$allowedtypes = array("image/png", "image/jpg", "image/jpeg", "image/bmp");
$errormsg = "";
if(!empty($error))
{
switch($error)
{
case '1':
$errormsg = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
break;
case '2':
$errormsg = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
break;
case '3':
$errormsg = 'The uploaded file was only partially uploaded';
break;
case '4':
$errormsg = 'No file was uploaded.';
break;
case '6':
$errormsg = 'Missing a temporary folder';
break;
case '7':
$errormsg = 'Failed to write file to disk';
break;
case '8':
$errormsg = 'File upload stopped by extension';
break;
default:
$errormsg = 'No error code avaiable';
}
} elseif(empty($tmpdir) || $tmpdir == 'none') {
$errormsg = 'No file was uploaded..';
} elseif(!in_array($type, $allowedtypes) || $size > $maxsize) {
$errormsg = 'Either image type not supported or size is extending 512 KB';
} else {
$filename = $_SESSION["username"];
$filename_array = explode(".",$_POST["filename"]);
$extension = array_pop($filename_array);
$path = 'folder/' . Utilities::GetDefaultPicturePath() . $filename . "." . $extension;
$location = $path . $filename;
move_uploaded_file($tmpdir, $location);
//for security reason, we force to remove all uploaded file
@unlink($file);
}
echo $errormsg;
?>
这是我的AJAX:
<form action="admin/upload_profile_picture.php" method="post" class="hide" enctype="multipart/form-data" id="form">
<input type="file" id="file" class="hide" name="file" />
<input type="submit" name="submit" id="submit" />
</form>
而作为形式隐藏在提交通过另一个按钮叫:
<button class="btn btn-primary" onclick='submitForm();'>Save changes</button>
和文件盒子被打开通过:
<button class="btn btn-primary" id="upload">Upload</button>
A
$("#upload").click(function() {
$("#file").click();
});
$("#save").click(function() {
$("#submit").click();
});
最后是ajaxForm()
jQuery插件脚本是在这里:ND这两个按钮的点击通过该脚本调用
$('#form').ajaxForm(function (data) {
console.log(data);
});
正如你可以看到,我记录返回的数据。我总是提交表单的是“没有文件上传..”。正如你可以在我的PHP页面上看到的那样,只有在文件的临时目录为空时才会打印该错误,并将其设置为“无”。
那么,什么是问题,我该如何解决这个问题。
谢谢。
如果您查看浏览器控制台的网络检查器,它是否显示正在上传的文件? –