Trying to make a simple script to comment on a news articles, it's working for the text but I would like the user to be able to submit a image to use as a icon or avatar. This is the form field:
<form action= "../create_comment.php" method="post" name="comments_form" enctype="multipart/form-data">
<div>
<label>Name<span>*</span></label>
<input name="name" type="text" value=" ">
</div>
<div>
<label>Email<span>*</span></label>
<input name="email" type="text" value=" ">
</div>
<div>
<label>Your Comment<span>*</span></label>
<textarea name="comment"> </textarea>
</div>
<div>
<label>Add your avatar<span>*</span></label>
<input name="file" type="file" />
<div>
<input name="storyid" value="192837465" type="hidden">
<input name="page_path" value="777776" type="hidden">
<input type="submit" value="submit">
</form>
and the form is directing to this script:
<?php
$formats = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ( $_FILES["file"]["size"] < 90000 && in_array($extension, $formats) ) {
if ($_FILES["file"]["error"] > 0) {
// something went wrong, display the error using; $_FILES["file"]["error"];
} else {
if ( !file_exists("avatars/" . $_FILES["file"]["name"] ) ) {
move_uploaded_file($_FILES["file"]["tmp_name"], "avatars/" . $_FILES["file"]["name"]);
}
}
}
$tempLink = "http://www.website.com/avatars/" . $_FILES["file"]["name"];
$page_path = $_POST['page_path'];
$con=mysqli_connect
("","","","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO comments (name, comment, email, storyid, entry_date)
VALUES
('$_POST[name]','$_POST[comment]','$_POST[email]','$_POST[storyid]',now())";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
header('Location: http://www.website.com/' . $page_path);
mysqli_close($con);
?>
The script runs to completion but the image doesn't get saved to:
www.website.com/avatars/
All the other form data inserts into SQL just fine. Only problem is the image. The targeted directory is chmod 0777.