我用ajax和jquery编写了在本地机器上上传图像的代码。如果条件没有被执行。
这是 fnUploadImage 脚本。
function fnUploadPhoto()
{
var image = $("#txtUploadImage").val();
var objUploadImage = new Object();
objUploadImage.ImageName = image;
objUploadImage.UserID = <?php echo $_SESSION['UserId']; ?>
$.ajax({
type:"POST",
url:"db.php?UploadImage",
data: {data:objUploadImage},
dataType:"json",
async:false,
success: function(response)
{
alert(response.ErrorMessage);
if(response.IsError)
$("#errorUploadPhoto")[0].innerHTML = response.ErrorMessage;
else
$("#errorUploadPhoto")[0].innerHTML = response.SuccessMessage;
},
error:function(response)
{
alert("Error: " + response.ErrorMessage);
}
});
}
该函数调用db.php UploadImage函数如下:
function UploadImage($data)
{
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["txtUploadImage"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
die(json_encode(array("IsError" => true, "ErrorMessage" => "Error")));
//echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
json_encode(array("IsError" => true, "ErrorMessage" => "Return Code: " . $_FILES["file"]["error"]));
}
else
{
if (file_exists("profile_images/" . $_FILES["file"]["txtUploadImage"]))
{
//echo $_FILES["file"]["name"] . " already exists. ";
json_encode(array("IsError" => true, "ErrorMessage" => "Filename already exists."));
}
else
{
if($_FILES["file"]["size"]/1024 < 20)
{
move_uploaded_file($_FILES["file"]["tmp_name"], "profile_images/" . $_FILES["file"]["txtUploadImage"]);
//echo "Stored in: " . "profile_images/" . $_FILES["file"]["name"];
json_encode(array("IsError" => false, "SuccessMessage" => "Image uploaded successfully."));
}
else
{
json_encode(array("IsError" => true, "ErrorMessage" => "Minimum image size is 20 kb."));
}
}
}
}
else
{
//echo "Invalid file.";
json_encode(array("IsError" => true, "ErrorMessage" => "Invalid file."));
}
}
当我运行此脚本时,它会在我身上显示名为 undefined 的错误。请帮我解决这个问题。