1

Why is this not working? Trying to upload file using PHP. File in question is an image just need to store the file path. Trying this code but is not working. Any help?

<html>
<body>
<form action="book_create.php" method="POST">
title: <input type="text" name="title"/><br>
authors: <input type="text" name="authors"/><br>
description: <textarea type="text" name="description"></textarea><br>
price: <input type="text" name="price"/><br>
image: <input type="file" name="image"/><br>
content: <input type="file" name="content"/><br>
<input type="submit" value="book_create"/>
</form>
</body>
</html>

The PHP:

if ($_FILES["image"]["error"] > 0)
{
echo "Error: " . $_FILES["image"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["image"]["name"] . "<br>";
echo "Type: " . $_FILES["image"]["type"] . "<br>";
echo "Size: " . ($_FILES["image"]["size"] / 1024) . " kB<br>";
echo "Stored in: " . $_FILES["image"]["tmp_name"];
 }

Keep getting undefined index error yet used "image"?

Thanks

user3453021
  • 93
  • 1
  • 3

2 Answers2

3

You're missing your enctype attribute on your form:

<form action="book_create.php" method="POST" enctype="multipart/form-data">
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

You need to include enctype="multipart/form-data" in your form tag

Vagabond
  • 877
  • 1
  • 10
  • 23