dpcnm2132 2014-12-14 01:33
浏览 38
已采纳

Laravel 4未定义变量:file_destination问题

Once I post the form with and image upload the first time it works. But when I do it again it gives me this error,

Undefined variable: file_destination

Here is all of my code that uses file_destination:

if(isset($_FILES['img'])) {
            $file = $_FILES['img'];

            // File properties
            $file_name = $file['name'];
            $file_tmp = $file['tmp_name'];
            $file_size = $file['size'];
            $file_error = $file['error'];

            // Work out the file extension
            $file_ext = explode('.', $file_name);
            $file_ext = strtolower(end($file_ext));

            $allowed = array('png', 'jgp', 'jpeg', 'gif');

            if(in_array($file_ext, $allowed)) {
                if($file_error === 0) {
                    if($file_size <= 180000000) {

                        $file_name_new = uniqid('', true) . '.'  . $file_ext;
                        $file_destination = 'img/content-imgs/' . $file_name_new;

                        if (move_uploaded_file($file_tmp, $file_destination)) {
                            echo '<img src="' .$file_destination. '">';
                        }

                    }
                }
            }
        }   

        $title  = Input::get('title');
        $slug   = Input::get('slug');
        $body   = Markdown::parse(Input::get('body'));
        $draft  = Input::get('draft');
        $created_at = date("Y-m-d H:i:s");
        $updated_at = date("Y-m-d H:i:s");

        $post = DB::table('posts')
            ->insert(array(
                'title' => $title,
                'slug' => $slug,
                'body' => $body,
                'img' => $file_destination,
                'draft' => $draft,
                'created_at' => $created_at,
                'updated_at' => $updated_at
        ));

Can someone please help me understand why I'm getting this error.

  • 写回答

1条回答 默认 最新

  • dongmacuo1193 2014-12-14 11:32
    关注

    As @Ali Gajani wrote in the comments. $file_destination is undefined when the first if

    if(isset($_FILES['img'])) {
    

    is false. So the whole code inside the if statement won't be executed. Especially the line where $file_destination gets declared.

    $file_destination = 'img/content-imgs/' . $file_name_new;
    

    The solution is simple. Just declare $file_destination before the if statement so it will be defined no matter what.

    $file_destination = null;
    
    if(isset($_FILES['img'])){
        // ...
        $file_destination = 'img/content-imgs/' . $file_name_new;
        // ...
    }
    
    // now $file_destination either is null or contains the path
    

    I chose null as default value. You could also use an empty string or something else. Only make sure your variables are defined.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?