Page 1 of 1

Image upload code not working

Posted: Sat Apr 25, 2009 11:39 pm
by anivad
Any idea why this doesn't work?

(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.');
}
}
}
?>
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.)

Re: Image upload code not working

Posted: Sat Apr 25, 2009 11:57 pm
by Yossarian
Turn on error reporting at the top of this script.

<?php
ini_set('display_errors', 1 );
error_reporting(E_ALL);
?>

Let us know what errors are being shown.

Start debugging this yourself by proving whether or not each line actually works.

Use var_dump() on each variable so you can see what is going on.

Start off with var_dump( $_POST ) and var_dump( $_FILES) and then move on to var_dump( $sql ) and make sure that sql statement gets correctly made.

When I learned about file uploads I found the best resource was the manual pages, print em off and read them line by line.

That is the good stuff.

Re: Image upload code not working

Posted: Sun Apr 26, 2009 1:29 am
by anivad
Thanks! Turned out that there was an undefined variable in one of the included files. Never gave me trouble before for some reason, but when I fixed that everything worked great. :)