cannot upload a file php
<p>Okay so I have an avatar upload function here:</p>

<pre><code>function avatar()
{
    global $db, $config, $core;

    if (isset($_POST['submit']))
    {
        // see if they currently have an avatar set
        $sql_avatar = "SELECT `avatar`, `avatar_uploaded` FROM `users` WHERE `user_id` = {$_SESSION['user_id']}";
        $query_avatar = $db-&gt;sqlquery($sql_avatar);
        $avatar = $db-&gt;fetch($query_avatar);

        if (isset ($_FILES['new_image']))
        {
            // check the dimensions
            list($width, $height, $type, $attr) = getimagesize($_FILES['new_image']['tmp_name']);

            if ($width &gt; $config['avatar_width'] || $height &gt; $config['avatar_height'])
            {
                $core-&gt;message('The dimensions are too big!');
            }               

            // check if its too big
            else if ($_FILES['new_image']['size'] &gt; 42000)
            {
                $core-&gt;message('File size too large!');
            }

            // this will make sure it is an image file, if it cant get an image size then its not an image
            else if (!getimagesize($_FILES['new_image']['tmp_name']))
            {
                $core-&gt;message('That is not an image!');
            }

            else
            {

                // give the image a random file name
                $imagename = rand() . 'id' . $_SESSION['user_id'] . '.jpg';

                // the actual image
                $source = $_FILES['new_image']['tmp_name'];

                // where to upload to
                $target = "uploads/avatars/" . $imagename;

                if (move_uploaded_file($source, $target))
                {
                    // remove old avatar
                    if ($avatar['avatar_uploaded'] == 1)
                    {
                        unlink('uploads/avatars/' . $avatar['avatar']);
                    }

                    $query = $db-&gt;sqlquery("UPDATE `users` SET `avatar` = ?, `avatar_uploaded` = 1, `avatar_gravatar` = 0, `gravatar_email` = '' WHERE `user_id` = ?", array($imagename, $_SESSION['user_id']));

                    return true;
                }

                else
                {
                    return false;
                }
            }
        }
    }
}
</code></pre>

<p>And it fails everytime and i have no idea why!
Here is how i check:</p>

<pre><code>else if ($_POST['action'] == 'Upload')
{
    if ($user-&gt;avatar() == true)
    {
        $core-&gt;message('Avatar uploaded!');
    }

    else
    {
        $core-&gt;message('Could not upload file!');
    }
}
</code></pre>

<p>Everytime it gives me the "could not upload file" error.</p>

<p>Can someone help me debug this?</p>

<p>The uploads/avatars folders exist.</p>
