dpkk8687 2014-03-24 07:45
浏览 12
已采纳

PHP图像上传失败

I really need your help. I am trying to upload an image with php code. When I use different .php file with only upload code, it works, but when I try to add to my main code where I have this information which is posted into database.

I have this added and as I said it works with separate code only for upload.

form action="core/books-reg.php" method="POST" enctype="multipart/form-data"

This is my main .php code in where I want to make it work. Code starts after !empty(Description). The database takes the name and puts in mysql but there is no file in my folder. any help or other method would be really good.

<?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");
}   
?>
  • 写回答

2条回答 默认 最新

  • douliao5942 2014-03-24 09:12
    关注

    if u want to place the upload code on same page then firstly you don't need to specify the action in the form tag.

    And check that the path upload/ is at the correct place exists or not.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作
  • ¥15 如何使用js实现打印时每页设置统一的标题
  • ¥15 安装TIA PortalV15.1报错
  • ¥15 能把水桶搬到饮水机的机械设计
  • ¥15 Android Studio中如何把H5逻辑放在Assets 文件夹中以实现将h5代码打包为apk
  • ¥15 使用小程序wx.createWebAudioContext()开发节拍器