move_uploaded_file不起作用?

时间:2018-05-25 02:03:54

标签: php

由于某些原因,我遇到了严重的问题, 我的函数move_uploaded_file将不起作用,并且不会返回错误(由try catch包围,并且phplog中没有错误)

我已经访问了很多谷歌链接,没有看到任何可以帮助我的东西,调试代码的所有行,并且看不到任何错误..所以我去找你,如果你能得到的话我!

非常感谢!!

if(isset($_FILES['avatar']) && !empty($_FILES['avatar']['name']))
        {
            $maxSize = 512000; // 50 Ko
            $validesExt = array('jpg', 'jpeg', 'png'); // Only jpg, jpeg or png

            if($_FILES['avatar']['size'] <= $maxSize)
            {
                $extUpload = strtolower(substr(strrchr($_FILES['avatar']['name'], '.'), 1)); // Get extension
                if(in_array($extUpload, $validesExt))
                {
                    $path = asset('boostpanel_assets/img/avatars/' . $_SESSION['user']['id'].".".$extUpload); // Upload the avatar
                    try {
                        $dep = move_uploaded_file($_FILES['avatar']['tmp_name'], $path); // move the file to the folder
                        if ($dep) {
                            $db->update('users', $_SESSION['user']['id'], 'id', [
                                'avatar' => $_SESSION['user']['id']
                            ]);
                        }
                    } catch (\Exception $e) {
                        die($e->getMessage());
                        $status = 'error';
                        $message = 'Oops! Something went wrong.';
                        return $this->redirect->route('profile')->with($status, $message);
                    }
                }else{
                    $status = 'error';
                    $message = 'This extension is not valid, only jpg, jpeg or png';
                    return $this->redirect->route('profile')->with($status, $message);
                }
            }else{
                $status = 'error';
                $message = 'Your avatar is too big.';
                return $this->redirect->route('profile')->with($status, $message);
            }
        }

1 个答案:

答案 0 :(得分:0)

我终于通过查看文档找到了问题! 对于每个需要的人:move_uploaded_file($ tmp,$ path):$ path需要是相对路径,而不是绝对路径。

我的函数asset()返回了绝对路径。

感谢。

相关问题