Image upload code not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Image upload code not working

Post 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.)
Last edited by Benjamin on Sun Apr 26, 2009 12:39 am, edited 1 time in total.
Reason: Changed code type from text to php.
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Re: Image upload code not working

Post 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.
anivad
Forum Commoner
Posts: 80
Joined: Thu Apr 09, 2009 11:16 pm

Re: Image upload code not working

Post 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. :)
Post Reply