0

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");
}   
?>
7
  • the path upload/ exists? have you write rights on that folder? try var_dump(is_dir('upload')); Commented Mar 24, 2014 at 7:52
  • yes, as I saidit works when upload code is in separate .php file Commented Mar 24, 2014 at 7:54
  • maybe the path from this PHP file is different since you use a relative path Commented Mar 24, 2014 at 7:55
  • Everything is fine. As I said, it parses the name to database, but no file is at the upload folder... Commented Mar 24, 2014 at 8:01
  • ponciste var_dump(is_dir('upload')); made no difference Commented Mar 24, 2014 at 8:03

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

No I want exactly the code to be in different page, because I have details, which should be send to mysql
0

Fixed with move_uploaded_file($_FILES["image"]["tmp_name"],"../upload/" . $_FILES["image"]["name"]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.