Image upload code not working
Posted: Sat Apr 25, 2009 11:39 pm
Any idea why this doesn't work?
(The error checking sections are okay; just the rest of it.)
The weird thing is that I keep getting to the loginsuccess page, which seems to indicate that the data is updated. But it's not.
I've been through so many online tutorials on image uploading via php and all of them have been unhelpful so far.
Help would be really appreciated, thanks!
(Alternatively if this doesn't work, how do I upload images into MySQL? They won't be big ones - just thumbnails, probably about 15kb in size each. Tutorials aren't helpful either.)
(The error checking sections are okay; just the rest of it.)
Code: Select all
<?php
include 'common.php';
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$avatar = stripslashes($_FILES['avatar']['tmp_name']);
$type = addslashes($_FILES['avatar']['type']);
$upload = "users/" . $avatar;
list($width, $height, $type, $attr) = getimagesize($avatar);
if($width > 100 || $height > 100) {
error ('Images must be within 100 x 100 pixels in size.');
}
elseif($type != 1 && $type != 2 && $type != 3) {
error ('Image must be a gif, jpg or png file.');
}
elseif($_FILES['avatar']['size'] > 51200) {
error ('Filesize too big. The maximum size for upload is 50kb.');
}
else {
include 'db.php';
is_uploaded_file($avatar);
$avatar = mysql_real_escape_string($avatar);
$uploaded = copy($avatar, $upload);
if ($uploaded) {
$sql = "UPDATE login SET avatar='$upload' WHERE uname='$uname'";
$result = mysql_query($sql);
if($result) {
header("Location: loginsuccess.htm");
}
else {
error('Error inserting data. Please try again.');
}
}
else {
error('Error uploading file. Please try again.');
}
}
}
?>I've been through so many online tutorials on image uploading via php and all of them have been unhelpful so far.
Help would be really appreciated, thanks!
(Alternatively if this doesn't work, how do I upload images into MySQL? They won't be big ones - just thumbnails, probably about 15kb in size each. Tutorials aren't helpful either.)