使用php上传默认头像

时间:2016-07-22 11:34:51

标签: php default avatar

当我不想上传头像并保留默认值“avatar1.jpg”时,它会不断给我同样的错误消息。 "文件类型无效!"可能有什么不对? 当我上传头像时,它可以正常工作。

此代码是函数,它位于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');
        }
    }

此代码正在获取功能,&#34; if($ user-&gt; uploadAvatar()){...}&#34;如果没有上传任何内容,则设置默认图片。

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

}

3 个答案:

答案 0 :(得分:1)

如果您没有上传avatar文件,那么与检查$_FILES["avatar"]["type"]时相比,您将始终收到错误的null == "image/gif" || null == "image/jpeg"...

首先通过执行if (!empty($_FILES['avatar']))

检查您是否拥有此文件

旁注您可以更改此代码:

($_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")

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

答案 1 :(得分:0)

你可以使用:

                    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);
                    }

答案 2 :(得分:0)

uploadAvatar的顶部,您需要在开始工作之前检查是否有要处理的文件。使用这些行

启动该功能
if(empty($_FILES['avatar']) ||
   !file_exists($_FILES['avatar']['tmp_name']) ||
   !is_uploaded_file($_FILES['avatar']['tmp_name'])
) return false;