0

Sorry for that question, but I tried everything and i can't solve my problem.

I'm trying to add an image to the database from a form in php and then show the image on the page.

However I am with an error I can't solve.

The error is this: Notice: Undefined index: image in C: \ xampp \ htdocs \ test \ index2.php on line 18

Does anyone could help me please?

Thanks everyone.

-----index.php-----------

    <html>
    <head>
        <title>Upload an image</title>
    </head>   
    <body>

        <form action="index2.php" method="POST" enctype="multipart/form-data">
             File:
            <input type="file" name="image"> <input type="submit" value="Upload">

        </form>

        <?php
if (isset($_FILES['image'])) {
mysql_connect("localhost", "root", "*****") or die (mysql_error());
mysql_select_db('database') or die (mysql_error());

$file = $_FILES['image']['tmp_name'];


if (!isset($file)) 
    echo "Please select an image";
else{
   echo $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
   $image_name = $_FILES['image']['tmp_name'];
   $image_size = getimagesize($_FILES['image']['tmp_name']);

   if($image_size==FALSE)
       echo "That's not an image";
   else{
            if(!$insert = mysql_query("INSERT INTO table_image(name_image, image) VALUES ('Test','$image' )"))
            echo"Problem uploading image.";
            else
            {
                $lastid = mysql_insert_id();
            echo"Image uploaded.<p />Your Image: <p /><img src=get.php?id=$lastid>";
            }

        }
}
}
?>

------get.php-----

   <?php

mysql_connect("localhost", "root", "*****") or die (mysql_error());
mysql_select_db('database') or die (mysql_error());

$id = addslashes($_REQUEST['id']);

$image = mysql_query("select * from table_image where id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/jpeg");
echo $image;


?>

enter image description here

Line 18: $file = $_FILES['image']['tmp_name'];

--------------EDIT----------------

thanks everyone for all sugestions. I tried everything you guys said. And the result was this.

enter image description here

What's going on?

7
  • $file = $_FILES['image']['tmp_name']; Commented Sep 26, 2014 at 13:44
  • Remove the echo in echo $image Commented Sep 26, 2014 at 13:45
  • Well the error, is just a notice.. Meaning there isn't a value for the key you are trying to call.. Commented Sep 26, 2014 at 13:45
  • 1
    Change if (isset($_POST['image'])) to if (isset($_FILES['image'])) Commented Sep 26, 2014 at 13:46
  • See Daan's answer below. That will fix your problem. Commented Sep 26, 2014 at 13:53

2 Answers 2

1

You forgot the 't' here.

enctype="mulipart/form-data"

so

enctype="multipart/form-data"

You're also trying to check for isset($_POST['image']) which should be isset($_FILES['image'])

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

10 Comments

Good catch. This if (isset($_POST['image'])) would also be an issue.
Thanks for reply. I tried what you said, and then give me that error "Problem uploading image."
You're trying to insert an uploaded file into a column. That won't work. You have to move you're file to a directory on the server with move_uploaded_file
...or set column as blob.
@Fred-ii- True but I try to avoid it. Actually I never use it.
|
0

apart from typo fix of mulipart/form-data to multipart/form-data You need to validate/check it before accessing

if (!empty($_FILES['image']['name'])) {
    $file = $_FILES['image']['tmp_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.