Upload not working
I have a form:
<form method="post" action="includes/tagcreate.php">
<input type="file" name="image" />
<input class="btn btn-orange" name="submit" type="submit" value="Create" />
</form>
And tagcreate.php:
require('connection.php');
if (isset($_POST["submit"])) {
$image = $_POST['image'];
$userfile_name = $_FILES["image"]["name"];
$large_image_location = '../';
$query = "INSERT INTO tags (image) VALUES ('{$image}')";
if (isset($_FILES["image"]["name"])){
move_uploaded_file($userfile_name, $large_image_location);
chmod ($large_image_location, 0777);
}
if (mysql_query($query, $connection)) {
header("location: ../");
exit;
} else {
echo "<p>Failed to add:&开发者_JAVA百科lt;/p>";
echo "<p>" . mysql_error() . "</p>";
}
}
So the database query is working fine but... no file. I haven't really dealt with uploads before so I feel like I might be missing something fairly obvious here.
To upload image you have to use enctype="multipart/form-data" in form ie
<form method="post" action="includes/tagcreate.php" enctype="multipart/form-data">
Also edit,
move_uploaded_file($_FILES["image"]["tmp_name"], $large_image_location);
You use this instead:
$userfile_name = $_FILES["image"]["tmp_name"];
$large_image_location = '../' . $_FILES["image"]["name"];
And add enctype="multipart/form-data"
to the form
element
include
enctype="multipart/form-data"
in your form tag. so it will
<form method="post" action="includes/tagcreate.php" enctype="multipart/form-data">
You need enctype="multipart/form-data"
:
<form method="post" action="includes/tagcreate.php" enctype="multipart/form-data">
Then you should change this line:
$userfile_name = $_FILES["image"]["name"];
To:
$userfile_name = $_FILES["image"]["tmp_name"];
Since the file is stored in the location in "tmp_name" not the location given by "name".
PS. If two users upload two different files with the same filename, the second user's will overwrite the file of the first user, since they have the same name. You could use [tempnam()][1]
to solve that.
精彩评论