私は、あなたが投稿を作成して、それに(ただ一つの)画像を持っているウェブアプリケーションを開発しようとしていますが、テキストポストだけでも構いません。これは、個人的なプロジェクトで、PHPとAJAXを使いこなすためのものです。私はこれを間違っているかもしれません...私がやろうとしているのは、フォームデータをアップロードするかどうかを決定するphp関数に送信することですそうでなければ、私はそれが成功した場合は、データベースに投稿を挿入するために別の関数に画像の名前を送信するが、画像のアップロードが成功しない場合、エラーを表示します。また、それは成功していませんが、エラーは '画像がアップロードされていません'、と進み、とにかく投稿を作成します。ここで画像がアップロードされたかどうかを確認するには? AJAX PHP
これまでのところ、私が持っているものである
jQueryの
$(".new-post").submit(function (e) {
e.preventDefault();
$.ajax({
type: "post",
url: "includes/image-upload.php",
data: new FormData(this),
processData: false,
contentType: false,
error: function (response) {
console.log(response);
},
success: function (response) {
var body = $("#post-body").val();
$.ajax({
type: "post",
url: "create-post-feed.php",
data: {
body: body,
image: response
},
error: function (response) {
console.log(response);
},
success: function (response) {
// console.log(response);
$('section.feed').prepend(response);
$('article.post p').each(function() {
$(this).html(linkHashtags($(this).html()));
});
$('article.post p').each(function() {
$(this).html(linkatsymbols($(this).html()));
});
revealPosts();
}
});
}
});
});
PHP は、いくつかの研究の後、私はそれが誤りである場合、私はJSONとしてそれを返送して、チェックする必要があることが分かりましたjQueryの結果しかし、私は...
<?php
require_once('../dbconnect.php');
include_once(INCLUDES_PATH .'functions.php');
function uploadFile ($file_field = null, $check_image = false, $random_name = false) {
//Config Section
$user_id = $_SESSION['user_id'];
//check if directory exist if not create it
if (!file_exists(HOME_PATH ."users/user_".$user_id)) {
mkdir(HOME_PATH ."users/user_".$user_id, 0777, true);
}
if (!file_exists(HOME_PATH ."users/user_".$user_id."/posts")) {
mkdir(HOME_PATH ."users/user_".$user_id."/posts", 0777, true);
}
//Set file upload path
$path = "../users/user_".$user_id."/posts/"; //with trailing slash
//Set max file size in bytes
$max_size = 1000000;
//Set default file extension whitelist
$whitelist_ext = array('jpeg','jpg','png','gif');
//Set default file type whitelist
$whitelist_type = array('image/jpeg', 'image/jpg', 'image/png','image/gif');
//The Validation
// Create an array to hold any output
$out = array('error'=>null);
if (!$file_field) {
$out['error'][] = "Please specify a valid form field name";
}
if (!$path) {
$out['error'][] = "Please specify a valid upload path";
}
if (count($out['error'])>0) {
return $out;
}
//Make sure that there is a file
if((!empty($_FILES[$file_field])) && ($_FILES[$file_field]['error'] == 0)) {
// Get filename
$file_info = pathinfo($_FILES[$file_field]['name']);
$name = $file_info['filename'];
$ext = $file_info['extension'];
//Check file has the right extension
if (!in_array($ext, $whitelist_ext)) {
$out['error'][] = "Invalid file Extension";
}
//Check that the file is of the right type
if (!in_array($_FILES[$file_field]["type"], $whitelist_type)) {
$out['error'][] = "Invalid file Type";
}
//Check that the file is not too big
if ($_FILES[$file_field]["size"] > $max_size) {
$out['error'][] = "File is too big";
}
//If $check image is set as true
if ($check_image) {
if (!getimagesize($_FILES[$file_field]['tmp_name'])) {
$out['error'][] = "Uploaded file is not a valid image";
}
}
//Create full filename including path
if ($random_name) {
// Generate random filename
$tmp = str_replace(array('.',' '), array('',''), microtime());
if (!$tmp || $tmp == '') {
$out['error'][] = "File must have a name";
}
$newname = $tmp.'.'.$ext;
} else {
$newname = $name.'.'.$ext;
}
//Check if file already exists on server
if (file_exists($path.$newname)) {
$out['error'][] = "A file with this name already exists";
}
if (count($out['error'])>0) {
//The file has not correctly validated
return $out;
}
if (move_uploaded_file($_FILES[$file_field]['tmp_name'], $path.$newname)) {
echo $newname;
} else {
$out['error'][] = "Server Error!";
}
} else {
$out['error'][] = "No file uploaded";
return $out;
}
}
$file = uploadFile('file', true, true);
if (is_array($file['error'])) {
echo json_encode($file['error']);
}
die();
を作業が本当にところでこれは、現在動作します それを改善する方法についての任意のヘルプやコメントに感謝することを取得するには至っていない、唯一の問題は、ポストは、常に画像をアップロードするかどうかに作成されていることですか否か。
通常、私は少なくとも同じ日に回答を得るでしょう...この質問は悪いですか? XD編集が「質問をバンプする」のに役立つと聞きました
オハイオウわかりました。私は複雑なものを作っていました...うーん、どうもありがとう! –
私はどのようにPHPのFormData()から本文の内容を取得するのですか? @Mikey –
AJAXで送信したデータを取得する方法を尋ねていますか?あなたが '$ _POST'と' $ _FILES'配列を見るのと同じように普通に行います。フォーム内のすべての入力に 'name'属性があることを確認してください。 – Mikey