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.

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

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?