我正在创建一个带有图片上传的博客。
这是我的 add_post 函数:
function add_post($title, $txt_content, $img_content, $tags, $file_temp, $file_extn){
$title = mysql_real_escape_string($title);
$txt_content = mysql_real_escape_string($txt_content);
$tags = mysql_real_escape_string($tags);
$file_path = 'simpleblog/resources/images/'. substr(md5(time()), 0, 20) . '.' . $file_extn;
move_uploaded_file($file_temp, $file_path);
mysql_query("INSERT INTO `posts` SET
`tag_id` = '$tags',
`title` = '$title',
`txt_content` = '$txt_content',
`img_content` = '". mysql_real_escape_string($file_path) ."',
`date_posted` = NOW()");
}
这是 add_post.php 的 php 脚本
<?php
include 'resources/init.php';
if (isset($_FILES['img_content']) === true){
if(empty($_FILES['img_content']['name']) === true){
echo '<script type = "text/javascript">window.alert("Please pick a file!");</script>';
} else {
$allowed = array('jpg','jpeg','gif','png');
$file_size = $_FILES['img_content']['size'];
$file_name = $_FILES['img_content']['name'];
$file_extension= explode('.', $file_name);
$file_extn= strtolower(end($file_extension));
$file_temp = $_FILES['img_content']['tmp_name'];
$title = $_POST['title'];
$txt_content = $_POST['txt_content'];
$tags = $_POST['tags'];
if(in_array($file_extn, $allowed) === true && isset($_POST['txt_content']) === true){
add_post($title, $txt_content, $tags, $file_temp, $file_extn);
$post_id = mysql_insert_id();
header("Location: index.php?post_id=" . $post_id);
exit();
}else if (in_array($file_extn, $allowed)=== false){
echo '<script type = "text/javascript">window.alert("The only file type allowed are .jpg, .gif, .png");</script>';
}
}
}
?>
mySQL 数据库使用图像文件的名称进行更新。但是,是不是把上传的文件搬进去了,有simpleblog/resources/images/
什么问题呢?