我的博客网站结构内的大家好,我有一个名为posts.php的页面,其中有一个表格可以让我添加新的帖子,除了图像不会显示在数据库中之外,其他所有内容都可以正常运行 但已上载到目标目录。如何在数据库中显示?这是用于处理add_post.php页面上的表单的代码/ * funtion的示例* / 函数add_post(){ 全局$ connection;
if (isset($_POST['submit'])) {
$file = $_FILES['file'];
$filename =$_FILES['file']['name'];
$fileTmpName =$_FILES['file']['tmp_name'];
$fileSize =$_FILES['file']['size'];
$post_title =$_POST['post_title'];
$post_content =$_POST['post_content'];
$post_author =$_POST['post_author'];
$post_category =$_POST['post_category'];
$post_category_id =$_POST['post_category_id'];
$post_status =$_POST['post_status'];
$date = date("l d F Y");
$post_views =0;
$post_comment_count=0;
$target_dir = "../images/";
$target_file = $target_dir . basename($filename);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
if ( $fileSize > 1000000) {
header("location:../posts.php?source=File is large");
exit();
$uploadOk = 0;
}else {
(move_uploaded_file($fileTmpName, $target_file));
}
$sql="INSERT INTO posts (post_title,post_content, post_image ,post_author,post_category,post_category_id,post_status,post_date, post_views,post_comment_count)
VALUES (?,?,?,?,?,?,?,?,?,?)";
$stmt= $connection->prepare($sql);
$stmt->bind_param("ssbssissii",$post_title, $post_content,$target_file ,$post_author, $post_category,$post_category_id,$post_status,$date,$post_views,$post_comment_count);
if (!$stmt->execute()) {
echo $connection->error;die;
}else {
header("Location:../posts.php?source=add_new");
}
}
}
add_post();`
答案 0 :(得分:0)
将图像保存在服务器上的公共目录中,并且在数据库中仅保存已保存图像的目录。使用此目录,您将生成一个URL来访问文件。