【发布时间】:2014-06-25 13:08:51
【问题描述】:
我收到此错误通知:未定义索引:图像。我正在尝试将图像上传到数据库。我不知道发生了什么,除了图片,其他所有内容都上传,甚至图片名称都没有上传到数据库
以下是文件大小只有 3kb 的代码。
echo $max_upload = (int)(ini_get('upload_max_filesize'));
echo "<pre>" . print_r($_POST, true) . "</pre>";
echo "<pre>". var_dump($_FILES['image']) . "</pre>";
print_r 显示为
Array
(
[title] => This is a post
[author] => Me
[keywords] => posting
[image] => url.jpg
[content] => this is a new post
[submit] => Publish now
)
vardump 显示为 NULL
以下是实际代码。
<html>
<head>
<title> inserting new posts </title>
</head>
<body>
<form method="post" action="insert_post.php" enctype="multipart/form-data/">
<table width="600" align="centre" border"10">
<tr>
<td> <h1> Insert New Post here </h1> </td>
</tr>
<tr>
<td> Post title <td>
<td> <input type="text" name="title" size="30"> </td>
</tr>
<tr>
<td> Post Author<td>
<td> <input type="text" name="author" size="30"> </td>
</tr>
<tr>
<td> Post keywords<td>
<td> <input type="text" name="keywords" size="30"> </td>
</tr>
<tr>
<td> Post image <td>
<td> <input type="file" name="image"> </td>
</tr>
<tr>
<td> Post Content <td>
<td> <textarea name="content" cols="20" rows="20" size="30"> </textarea> </td>
</tr>
<tr>
<td> Post title <td>
<td> <input type="text" name="title" size="30"> </td>
</tr>
<tr>
<td> <input type="submit" name="submit" value="Publish now"> </td>
</tr>
</table>
</form>
</body>
</html>
<?php
include('../includes/connect.php');
if(isset($_POST['submit'])){
echo $post_title=$_POST['title'];
echo $post_date=date('d-m-y');
echo $post_author=$_POST['author'];
echo $post_keywords=$_POST['keywords'];
echo $post_content=$_POST['content'];
echo $post_image=$_FILES['image']['name'];
echo $image_tmp=$_FILES['image']['tmp_name'];
if($post_title=='' or $post_keywords=='' or $post_content=='' or $post_author==''){
echo "<script> alert('none of the fields can be empty')</script>";
exit();
} else{
move_uploaded_file($image_tmp,"../images/$post_image");
//query is below to insert data
$insert_query="INSERT into posts
(post_title,post_date,post_author,post_image,post_keywords,post_content)
VALUES('$post_title','$post_date','$post_author','$post_image','$post_keywords','$post_content')";
if(mysqli_query($connect,$insert_query)){
echo " <h1> successfully Posted </h1> ";
} else " h1> Did not work </h1>";
}
}
echo $max_upload = (int)(ini_get('upload_max_filesize'));
echo "<pre>" . print_r($_POST, true) . "</pre>";
echo "<pre>". var_dump($_FILES['image']) . "</pre>";
?>
【问题讨论】:
-
你有一个拼写错误,
enctype="multipart/form-data/应该是enctype="multipart/form-data -
您打印
$_POST,但使用var_dump($_FILES)。请注意,您对 SQL 注入和 PHP 脚本的上传持开放态度。