【问题标题】:The file was not uploaded due to an unknown error由于未知错误,文件未上传
【发布时间】:2019-04-18 02:14:22
【问题描述】:

我正在尝试为多个学生上传同一张图片。它适用于第一个,但对于下一个它会出错。 我的代码

foreach($student_ids as $key => $student_id) {

        $fileIds='';
        if(Input::hasfile('attachment')){
            $comment = $comments[0];
            $file = Input::file('attachment');
            $destinationPath = public_path().'/uploads/moments/'.$student_id;
            $filename = "moment_".time()."_".trim(rand(1,999)).".".$file->getClientOriginalExtension();
            if(is_dir($destinationPath)) {
                $upload_success =  $file->move($destinationPath, $filename);
            }else {
                if(mkdir($destinationPath)) {
                    $upload_success =  $file->move($destinationPath, $filename);    
                }
            }
            $file = Moment_gallery::create(['image'=>$filename,'comment'=>$comment]);
            $fileIds.=$file->id.',';                        
        }

        $moments = new moment();
        $moments->activity_id = $activity;
        $moments->type=$activity_type;
        $moments->student_id=$student_id;           
        $moments->date = $date;
        $moments->time = $activity_time ? $activity_time : ' ';
        $moments->save();
    }

在这里,我正在获取多个学生 ID 的数组...所以我只是为这些学生上传相同的图像。它适用于单个学生,但随后适用于多个学生,它适用于第一个学生并为第二个学生提供错误

FileException in UploadedFile.php line 235:The file "student.png" was not uploaded due to an unknown error.

怎么了..?谢谢。

【问题讨论】:

    标签: php laravel-5


    【解决方案1】:
    $file = Input::file('attachment');
    

    了解 $file 是对象类型:

    Illuminate\Http\UploadedFile
    

    并且它指向服务器上的文件,例如

    e‌‌cho $file->getRealPath(); // ‌/tmp/phpp6HYIk
    

    然后你可以通过执行看到:

    $file->move($dest_path);
    

    您正在将文件从其原始目标位置移开,从而使其无法用于后续迭代。

    【讨论】:

      猜你喜欢
      • 2017-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-02
      • 2020-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多