4

I'm trying to upload my files to the profile/ directory on my server and I believe everything may be working... however on upload it thinks that my jpeg png and gifs are not the correct file type. Why is it doing this. What's wrong here? How do i fix it?

function change_profile_image($user_id, $file_temp, $file_extn) {
$file_path = 'profile/' . substr (md5(time()), 0, 10) . '.' . $file_extn;
move_uploaded_file($file_temp, $file_path);
mysql_query("UPDATE `users` SET `profile` = " . mysql_real_escape_string($file_path) . "' WHERE `user_id` = " . (int)$user_id);

}

 if (isset($_FILES['profile']) === true) {
    if (empty($_FILES['profile']['name']) === true) {
       echo 'y u no choose file!';
  } else {
       $allowed = array ('jpg', 'jpeg', 'gif', 'png');
       //this is the part i think may be brocken.
       $file_name = $_FILES['profile']['name'];
       $file_extn = strtolower(end(explode (' . ', $file_name)));
       $file_temp = $_FILES['profile']['tmp_name'];

       if (in_array($file_extn, $allowed) === true) {
        change_profile_image($session_user_id, $file_temp, $file_extn);

        header('Location: dontdelete.php');
        exit();

       }else {
        echo 'y u no jpg or png or gif';       

       }
  }
 }

if (empty($user_data['profile']) === false) {
    echo '<img src"', $user_data['profile'], '" alt="">'; 
}
user1775570
  • 413
  • 4
  • 11
  • [Please, don't use `mysql_*` functions in new code](http://stackoverflow.com/q/12859942). They are no longer maintained and the deprecation process has begun, see the [red box](http://php.net/mysql-connect). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, – Geek Num 88 Nov 13 '12 at 01:37
  • Defiantly will get around to that eventually! – user1775570 Nov 13 '12 at 01:39

1 Answers1

3

Change explode (' . ', $file_name) to explode ('.', $file_name)

also you need to check its a valid image and not just ending in an image extension.

Your also missing = in your img output and you may as well concatinate instead of echoing 3 times:

echo '<img src="'.$user_data['profile'].'" alt="">';

Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106