0

Uploading an image into a directory, it seems to always try upload "" which is nothing. Rather then an actual image. What am I doing below. I know the value is "" because of what it inserts into the database.

<li class="formProductImage">
<span>Image:</span>
<input type="file" name="productImage" /> 
</li>

and the php:

if ($_FILES["productImage"]["error"] > 0)
    {
        echo "Return Code: " . $_FILES["productImage"]["error"] . "<br />";
    }
else
    {
    echo "Upload: " . $_FILES["productImage"]["name"] . "<br />";
    echo "Type: " . $_FILES["productImage"]["type"] . "<br />";
    echo "Size: " . ($_FILES["productImage"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["productImage"]["tmp_name"] . "<br />";
    $filename = mysql_real_escape_string($_FILES['productImage']['name']);

      $query = "UPDATE products SET image = '$filename' WHERE name = '$name'";
      $result = mysql_query($query);

        if (mysql_affected_rows() == 1) {
            // Show thank you message
            echo '<span style="color:green;">Your image was added to database correctly.</span>';

            if (file_exists("uploaded/" . $_FILES["productImage"]["name"]))
            {
                echo $_FILES["productImage"]["name"] . " already exists. ";
            }
            else
            {
                move_uploaded_file($_FILES["productImage"]["tmp_name"],
                "uploaded/" . $_FILES["productImage"]["name"]);
                echo "Stored in: " . "uploaded/" . $_FILES["productImage"]["name"];
            }

        } else {
            echo '<font color="red">Image note inserted into database.</font>';
            echo mysql_error();
        }

    }
                    }

                        }

2 Answers 2

2

Make sure you have enctype="multipart/form-data" in the <form> tag.

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

2 Comments

woohoo it works you legend, such a simple thing I missed from this book I am reading lol sigh! time to go opticians i think haha.
Hehe, I did the same mistake myself once and now I always think about it when creating file uploads :)
1

change this line:

$filename = mysql_real_escape_string($_FILES['file']['name']);

to :

$filename = mysql_real_escape_string($_FILES['productImage']['name']);

1 Comment

good spot but this hasn't solved the issue. Doesn't solve the "" string in the db either.

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.