Danny
Danny

Reputation: 27

Uploading default avatars with php

It keeps giving me same error message when I don't want to upload an avatar and keep a default 'avatar1.jpg'. "Invalid File Type!" What could be wrong? When I upload an avatar, it works fine.

This code is the function which stays in the functions.php

public function uploadAvatar(){
        $allowedExts = array("gif", "jpeg", "jpg", "png");
        $temp = explode(".", $_FILES["avatar"]["name"]);
        $extension = end($temp);
        if ((($_FILES["avatar"]["type"] == "image/gif")
                || ($_FILES["avatar"]["type"] == "image/jpeg")
                || ($_FILES["avatar"]["type"] == "image/jpg")
                || ($_FILES["avatar"]["type"] == "image/pjpeg")
                || ($_FILES["avatar"]["type"] == "image/x-png")
                || ($_FILES["avatar"]["type"] == "image/png"))
                && ($_FILES["avatar"]["size"] < 1000000)
                && in_array($extension, $allowedExts)) {
            if ($_FILES["avatar"]["error"] > 0) {
                redirect('register.php', $_FILES["avatar"]["error"], 'error');
            } else {
                if (file_exists("images/avatars/" . $_FILES["avatar"]["name"])) {
                    redirect('register.php', 'File already exists', 'error');
                } else {
                    move_uploaded_file($_FILES["avatar"]["tmp_name"],
                    "images/avatars/" . $_FILES["avatar"]["name"]);

                    return true;
                }
            }

        } 

        else {
            redirect('register.php', 'Invalid File Type!', 'error');
        }
    }

This code is getting the function, " if($user->uploadAvatar()){ ... } "to set up a default image if nothing is uploaded.

if(isset($_POST['register-input'])){
    //Create Data Array
    $data = array();
    $data['name'] = $_POST['name'];
    $data['email'] = $_POST['email'];
    $data['username'] = $_POST['username'];
    $data['password'] = md5($_POST['password']);
    $data['password2'] = md5($_POST['password2']);
    $data['about'] = $_POST['about'];
    $data['last_activity'] = date("Y-m-d H:i:s");

    //Required Fields
    $field_array = array('name','email','username','password','password2');

        if($validate->isRequired($field_array)){
            if($validate->isValidEmail($data['email'])){
                if($validate->passwordsMatch($data['password'],$data['password2'])){
                        //Upload Avatar Image
                        if($user->uploadAvatar()){
                            $data['avatar'] = $_FILES["avatar"]["name"];
                        }   else{

                            $data['avatar'] = 'avatar1.jpg';
                        }

                        //Register User
                        if($user->register($data)){
                            redirect('index.php', 'You are registered and can now log in', 'success');
                        } else {
                            redirect('index.php', 'Something went wrong with registration', 'error');
                        }
                } else {
                    redirect('register.php', 'Your passwords did not match', 'error');
                }
            } else {
                redirect('register.php', 'Please use a valid email address', 'error');
            }
        } else {
            redirect('register.php', 'Please fill in all required fields', 'error');
        }

}

Upvotes: 0

Views: 1425

Answers (3)

BeetleJuice
BeetleJuice

Reputation: 40896

At the top of uploadAvatar, you need to check whether there is a file to process before you start the work. Start the function with these lines

if(empty($_FILES['avatar']) ||
   !file_exists($_FILES['avatar']['tmp_name']) ||
   !is_uploaded_file($_FILES['avatar']['tmp_name'])
) return false;

Upvotes: 0

Justinas
Justinas

Reputation: 43479

If you don't upload avatar file, than when checking for $_FILES["avatar"]["type"] you will always receive false null == "image/gif" || null == "image/jpeg"....

First check if you have this file by doing if (!empty($_FILES['avatar']))


Side note You can change this code:

($_FILES["avatar"]["type"] == "image/gif")
    || ($_FILES["avatar"]["type"] == "image/jpeg")
    || ($_FILES["avatar"]["type"] == "image/jpg")
    || ($_FILES["avatar"]["type"] == "image/pjpeg")
    || ($_FILES["avatar"]["type"] == "image/x-png")
    || ($_FILES["avatar"]["type"] == "image/png")

to

in_array($FILES['avatar']['type'], ["image/gif", "image/jpeg", "image/pjpeg", "image/x-png", "image/png"])

Upvotes: 1

Ehsan
Ehsan

Reputation: 459

you can use :

                    if($user->uploadAvatar()){
                        $data['avatar'] = $_FILES["avatar"]["name"];
                    }   else{

                       $file =      'avatar1.jpg';

                       // Open the file to get existing content
                      $data = file_get_contents($file);

                     // New file
                        $new = 'new_name.jpg';

                     // Write the contents back to a new file
                      file_put_contents($new, $data);
                    }

Upvotes: 0

Related Questions