我使用以下php代码上传图片,但文件没有上传。
if(isset($_POST['submit'])){
$title = $_POST['title'];
$target_folder = "../newsimageuploads/";
$bannerimagelink = "http://example.com/newsimageuploads";
$bannerimage = addslashes(file_get_contents($_FILES['bannerimage']['tmp_name']));
$bannerimage_name = addslashes($_FILES['bannerimage']['name']);
$bannerimage_size = getimagesize($_FILES['bannerimage']['tmp_name']);
if ($bannerimage!=""){
$rand = rand(111111, 9999999);
$fname = $_FILES["bannerimage"]["name"];
$newname = "Image ".$rand.".png";
move_uploaded_file($_FILES["bannerimage"]["tmp_name"], $target_folder.$newname);
$bannerimage_location = $bannerimagelink."/".$newname;
}
$query =mysql_query("INSERT INTO mytable (title,image) VALUES ('$title','$bannerimage_location')")or die(mysql_error());
if (($query) === TRUE) {
echo "<p style='color:green;'>Added Successfully</p>";
} else {
echo "Some Error Occured :(";
}
}
我的HTML部分是
<form action="#" method="post">
<input type="text" name="title">
<input type="file" name="bannerimage" accept="image/jpeg,image/png,image/gif">
<button type="submit" name="submit">Add</button>
</form>
我的标题是在MySQL表中插入,但图像没有。
答案 0 :(得分:1)
您的表单中缺少enctype='multipart/form-data'
<form action="#" method="post" enctype="multipart/form-data">
查看here了解更多详情
答案 1 :(得分:1)
添加
enctype="multipart/form-data"
到表单标记。如果没有此属性,您将只获取文件的名称。但是文件本身不会上传。
答案 2 :(得分:1)
<form action="#" method="post" enctype="multipart/form-data">
<input type="text" name="title">
<input type="file" name="bannerimage" accept="image/jpeg,image/png,image/gif">
<button type="submit" name="submit">Add</button>