Domanda

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

È stato utile?

Soluzione

You're missing your enctype attribute on your form:

<form action="book_create.php" method="POST" enctype="multipart/form-data">

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top