doulei1965 2016-03-23 16:22
浏览 8

无法将图像上传到服务器

For some reason the function set_file() and save_profile_pic() just don't seem to actually be running and I can't figure out why. The functions themselves work fine as they work when creating a new user, but for some reason they don't work here.

I have also tried this:

if(!$user -> set_file($_FILES['profile_pic'])) {
    redirect("somewherenonexistant.php");
}

and also:

if($user -> set_file($_FILES['profile_pic'])) {
    redirect("somewherenonexistant.php");
}

Just to see whether set_file() is actually working, but the code is never run... Regardless of whether I test set_file() being true or false, it never gets to that redirect() line... Surely it must be either true or false?!

I literally have absolutely no idea why the code isn't run. Presumably I've been staring at it for too long now and just can't find it, so I really need some help.

Here's the code:

if(!empty($_GET['id'])) {
    $user = User::get_by_id($_GET['id']);

    if(isset($_POST['update'])) {
        if($user) {
            $user -> username = $_POST['username'];
            $user -> password = $_POST['password'];
            $user -> first_name = $_POST['first_name'];
            $user -> last_name = $_POST['last_name'];

            if(!empty($_FILES['profile_pic']['size'])) {
                $user -> set_file($_FILES['profile_pic']);
                $user -> save_profile_pic();
                $user -> save();

                redirect("edit_user.php?id={$user -> id}");
            } else {
                $user -> save();
            }
        }
    }
} else {
    redirect('users.php');
}

and this is the set_file() function:

public function set_file($file) {
        if(empty($file) || !$file || !is_array($file)) {
            $this -> errors[] = 'No file was uploaded';
            return false;
        } else if($file['error'] != 0) {    //!= 0 == UPLOAD_ERR_OK
            $this -> errors[] = $this -> upload_errors[$file['error']];
            return false;
        } else {
            $this -> profile_pic = basename($file['name']);
            $this -> tmp_path = $file['tmp_name'];
        }

        return true;
}

Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答