我真的需要你的帮助。我试图用PHP代码上传图像。当我使用不同的.php文件只有上传代码时,它可以工作,但是当我尝试添加到我的主代码中时,我将这些信息发布到数据库中。
我添加了这个,正如我所说,它仅适用于上传的单独代码。
form action="core/books-reg.php" method="POST" enctype="multipart/form-data"
这是我想要使其工作的主要.php代码。代码在!empty(描述)之后开始。数据库采用名称并放入mysql,但我的文件夹中没有文件。任何帮助或其他方法都会非常好。
<?php
session_start();
require("dbc.php");
$username = $_SESSION['username'];
$title = $_POST['title'];
$con = $_POST['con'];
$price = $_POST['price'];
$prodcode = $_POST['prodcode'];
$isbn = $_POST['isbn'];
$publisher = $_POST['publisher'];
$pages = $_POST['pages'];
$description = $_POST['description'];
$year = $_POST['year'];
$course = $_POST['course'];
$module = $_POST['module'];
if(!empty($title))
{
if(!empty($price))
{
if(!empty($prodcode))
{
if(!empty($isbn))
{
if(!empty($publisher))
{
if(!empty($pages))
{
if(!empty($description))
{
if (!isset($_FILES['image']['tmp_name'])) {
echo "";
}else{
$file=$_FILES['image']['tmp_name'];
$image= addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name= addslashes($_FILES['image']['name']);
move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $_FILES["image"]["name"]);
$location="upload/" . $_FILES["image"]["name"];
}
mysql_query("INSERT INTO `books` (username,title,con,price,prodcode,isbn,publisher,pages,description,image,year,course,module) VALUES('$username','$title','$con','$price','$prodcode','$isbn','$publisher','$pages','$description','$image_name','$year','$course','$module')");
header("location: ../add_books.php?feedback=Your book has been put on the books page.");
}
else
{
header("location: ../add_books.php?feedback=Description field is empty");
}
}
else
{
header("location: ../add_books.php?feedback=Pages field is empty");
}
}
else
{
header("location: ../add_books.php?feedback=Publisher field is empty");
}
}
else
{
header("location: ../add_books.php?feedback=ISBN field is empty");
}
}
else
{
header("location: ../add_books.php?feedback=Product code field is empty");
}
}
else
{
header("location: ../add_books.php?feedback=Price field is empty");
}
}
else
{
header("location: ../add_books.php?feedback=Title field is empty");
}
?>
答案 0 :(得分:1)
如果您想将上传代码放在同一页面上,那么首先您不需要在action
代码中指定form
。
并检查路径upload/
是否存在于正确的位置。
答案 1 :(得分:0)
使用move_uploaded_file修复($ _ FILES [&#34;图像&#34;] [&#34; tmp_name&#34;],&#34; ../ upload /&#34;。$ _FILES [&#34 ;图像&#34;] [&#34;名称&#34;]);