嗨伙计们我需要你的帮助我不是很好用ajax我试图上传一个图像使用PHP的形式图像是没有得到的PHP我有错误
Notice: Undefined index: ImageFile in line 9
这一行是
move_uploaded_file($_FILES['ImageFile']['tmp_name'], "$Destination/$NewImageName");
这是我的jquery
$("#send_friend").click(function(e) {
e.preventDefault();
$("#message").empty();
$('#loading').show();
// getting the values that user typed
var add_friends = '1';
var friend_name = $("#friend_name").val();
var friend_url = $("#friend_url").val();
var ImageFile = $('#ImageFile').prop('files')[0];
// forming the queryString
var data=
'add_friends=' + add_friends +
'&clan_name=' + clan_name +
'&friend_name=' + friend_name +
'&friend_url=' + friend_url +
'&ImageFile=' + ImageFile;
// ajax call
$.ajax({
type: "POST",
url: "components/friends.php",
data: data,
success: function(data){ // this happen after we get result
$('#loading').hide();
$("#message").html(data);
}
});
return false;
});
这是我的php
if(isset($_POST['add_friends'])){
include "../_database/database.php";
$Destination = '../imagenes/friends';
$friend_name = $database ->real_escape_string(htmlentities($_REQUEST['friend_name']));
$friend_url = $database ->real_escape_string(htmlentities($_REQUEST['friend_url']));
if(!isset($_FILES["ImageFile"]) || !is_uploaded_file($_FILES["ImageFile"]["tmp_name"])){
$NewImageName= 'default.png';
move_uploaded_file($_FILES["ImageFile"]["tmp_name"], "$Destination/$NewImageName");
}
else{
$RandomNum = rand(0, 999);
$ImageName = str_replace(' ','-',strtolower($_FILES['ImageFile']['name']));
$ImageType = $_FILES["ImageFile"]["type"];
$ImageExt = substr($ImageName, strrpos($ImageName, '.'));
$ImageExt = str_replace('.','',$ImageExt);
$ImageName = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
$NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;
move_uploaded_file($_FILES["ImageFile"]["tmp_name"], "$Destination/$NewImageName");
}
$sql = "INSERT INTO family (name,url, shield)
VALUES ('".$friend_name."', '".$friend_url."', '".$NewImageName."')";
$r1 = mysqli_query($database,$sql);
$sqlResult = $r1;
/*** run the sql statement ***/
}
和我的HTML
<div class="form-group">
<label for="exampleInputFile" class="supercell">Shield</label>
<input type="file" id="ImageFile" name="ImageFile"/>
只有文件我有一些问题,数据库已填充所有其他vars但无法将文件传递给php任何帮助将非常感谢
答案 0 :(得分:0)
您需要将contentType设置为multipart / form-data,因为表单包含文件。
type: "post",
contentType: "multipart/form-data"