خوب این کاری که شما میکنید واسه ارسال فایل به صورت ایجکس عملی نیست...
به این صورت باید عمل کنید:
//اینجا آی دی اون فایل رو به جای task-file میارید...
var fileData = $('#task-file').prop('files')[0];
var formData = new FormData();
formData.append('file', fileData);
formData.append('anotherData', likeUserName);
formData.append('anotherData2', likeEmail);
$.ajax({
url: URL + 'ajaxes/upload.php',
type: 'POST',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
if(myXhr.upload){ // Check if upload property exists
myXhr.upload.addEventListener('progress',progressH andlingFunction, false); // For handling the progress of the upload
}
return myXhr;
},
//Ajax events
beforeSend: function(){
},
success: function(data){
alert(data+'success upload and insert');
},
cache: false,
data: formData,
dataType:'json',
contentType: false,
processData: false,
error:function(){
alert('error ajax');
}
});
فایل upload هم به این صورت:
if (isset($_FILES["file"]) && isset($_POST["anotherData"]) && isset($_POST["anotherData2"])) {
$taskId = $_POST['anotherData'];
$uploadDirectory = DIR.'tasksFile/'; //specify upload directory ends with / (slash)
//Is file size is less than allowed size.
if ($_FILES["file"]["size"] > 5242880) {
die("-2");
}
$fileName = strtolower($_FILES['file']['name']);
$suffix = substr($fileName, strrpos($fileName, '.')); //get file extention
$rand= rand(1, 9999);
$newName = time() . $rand . $suffix; //new file name
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadDirectory . $newName)) {
$id=$sqlOPR->insert('file',"name=$newName");
$file['name']=$newName;
$file['fileId']=$id['id'];
echo json_encode($file);
} else { $file['error']='not uploaded';
echo json_encode($file); }
}
تابعی که اینسرت رو انجام میده از کلاسهای خودمه اشتباه نگیرید یه وقت... مقادیری هم که برگشت میده صفحه رو به عنوان مثاله اگر نمیخواید حذفشون کنید...