我不知道我要去哪里。我知道我是一个业余爱好者,我的代码也不是很好。我的JQuery在这里发送数据,但似乎无法获取存储的数据和文件。我在JQuery中使用了FormData()。这是我的代码:
if (isset($_POST['audio_lesson'])) {
$title = preg_replace("#[<>]#i", "", $_POST['title']);
$description = preg_replace("#[<>]#i", "", $_POST['description']);
$category = preg_replace("#[<>]#i", "", $_POST['category']);
$tags = $_POST['tags'];
//$font = preg_replace("#[^0-9]#i", "", $_POST['font']);
echo $post->Post($_FILES["post_files"], $title, $description, $category, $tags);
}
并且:
public function Post($post_files, $title, $description, $category, $tags){
$session = $_SESSION['id'];
$universal = new universal();
$name = preg_replace("#[\'\"]#i", "", $post_files['name']);
$size = $post_files['size'];
$tmp_name = $post_files['tmp_name'];
$error = $post_files['error'];
$ext = strtolower(end(explode(".", $name)));
$allowed = array("mp3");
if (in_array($ext, $allowed)) {
if ($error == 0) {
$new_name = time().".".$ext;
if (move_uploaded_file($tmp_name, "../../folder/".$session."/audio/"."rand_".$name)) {
$query = $this->db->prepare("INSERT INTO post (user_id, type, time, about, description, category, tags) VALUES (:user, :type, now(), :about, :description, :category, :tags)");
$query->execute(array(":user" => $session, ":type" => "audio", ":about" => $title,":description" => $description, ":category" => $category, ":tags" => $tags));
}
} else {
return "Error!!";
}
}
}
为了避免违反Stacked的发布政策和限制,我将HTML表单和JQuery / AJAX代码添加到了代码笔中。
请在此处的https://codepen.io/masoodbawa/pen/VwZBvWB
中找到此Codepen中的HTML表单和AJAX。我需要表单数据来提交并存储在数据库中,还需要将文件上传到目录中。
有什么想法吗? TIA