How to fix the error [The file was not uploaded due to an unknown error.]

31 Views Asked by At

My form is dynamic with multiple inputs and multiple images also can be uploaded. without selecting the images and inserting any amount of records without any error it's inserting to the table but If I select multiple images for each row first loop records are inserting images also uploading without any error but the second records loop not uploading it's giving the error called.

The file "Royal.jpg" was not uploaded due to an unknown error.

Royal.jpg This image I selected for the first loop and second loop I selected a different image but I don't why this error is coming.

Please help me fix this issue.

Form HTML with Foreach Loop

@foreach($lmsSection as $lmsSection)
                                          <div class="col-sm-10">
                                            <div class="dynamic-wrap">
                                              <div id="sectionForm">
                                                <div class="entry input-group">
                                                    <div class="col-md-2">
                                                        <div class="form-group">
                                                        <input class="form-control" name="lesson_title_edit[]" type="text" placeholder="Lesson" value="{{$lmsSection->lesson_title}}" id="lesson_title_edit"/>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-2">
                                                        <div class="form-group">
                                                        <input class="form-control" name="link_edit[]" type="text" placeholder="Links" value="{{$lmsSection->link}}" id="link_edit"/>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-2">
                                                        <div class="form-group">
                                                        <input class="form-control" name="lesson_password_edit[]" type="text" placeholder="Password" value="{{$lmsSection->lesson_password}}" id="lesson_password_edit"/>
                                                        </div>
                                                    </div>
                                
                                                    <div class="col-md-2">
                                                        <div class="form-group">
                                                        <input class="form-control" type="file" name="lesson_doc_edit[]" id="lesson_doc_edit" multiple>
                                                        <input class="form-control" name="lesson_doc_edit_old[]" type="hidden" value="{{$lmsSection->lesson_doc}}" id="lesson_doc_edit_old"/>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-2">
                                                        <div class="form-group">
                                                        <input class="form-control" name="lesson_quiz_edit[]" type="text" placeholder="" value="{{$lmsSection->lesson_quiz}}"/>
                                                        </div>
                                                    </div>
                                                    <div class="col-md-2">
                                                        <div class="form-group">
                                                        <input class="form-control" name="ins_note_edit[]" type="text" placeholder="Ins Note" id="ins_note_edit" value="{{$lmsSection->ins_note}}"/>
                                                        </div>
                                                    </div>
                                                  <span class="input-group-btn">
                                                    <button class="btn btn-danger" type="button" style="margin-left: 935px;top: -50px;">
                                                        <a href="javascript:void(0);" onclick="$('#LessonDeleteForm{{$lmsSection->id}}').submit()" style="color:#FFF;"><span class="glyphicon glyphicon-minus"></span></a>
                                                      
                                                    </button>
                                                  </span>
                                                </div>
                                              </div>
                                            </div>
                                          </div>
                                          @endforeach

This is my controller code

foreach ($request->input('lesson_title_edit') as $key => $lessonTitle) {
            $fileNames = [];
            
            // Retrieve other form data
            $link = $request->input('link_edit')[$key];
            $lessonPassword = $request->input('lesson_password_edit')[$key];
            $lessonQuiz = $request->input('lesson_quiz_edit')[$key];
            $lessonNote = $request->input('ins_note_edit')[$key];
        
            // Create the folder path
            $folderNameEdit = $batchCode;
            $folderPathEdit = 'lms_section/' . $folderNameEdit . "/" . $lessonTitle;
        
            // Create the folder if it doesn't exist
            if (!file_exists(public_path($folderPathEdit))) {
                mkdir(public_path($folderPathEdit), 0777, true); // The third parameter true creates nested directories
            }
        
            // Set the folder permissions (adjust as needed)
            chmod(public_path($folderPathEdit), 0777);
        
            // Process uploaded files
            if ($request->hasFile('lesson_doc_edit')) {
                foreach ($request->file('lesson_doc_edit') as $uploadedFile) {
                    if ($uploadedFile !== null) {
                        // Generate a unique file name
                        $newNameEdit = $lessonTitle . "_" . uniqid() . '.' . $uploadedFile->getClientOriginalExtension();
        
                        // Move the uploaded file to the folder
                        if ($uploadedFile->move(public_path($folderPathEdit), $newNameEdit)) {
                            $fileNames[] = $newNameEdit;
                        } else {
                            dd("File move failed"); // Debugging information
                        }
                    }
                }
            }
        
            // Check if any files were uploaded
            if (!empty($fileNames)) {
                // Convert the array $fileNames into a string separated by commas
                $newPathsStringEdit = implode(',', $fileNames);
            } else {
                $newPathsStringEdit = $request->input('lesson_doc_edit_old')[$key] ?? '';
            }
        
            // Insert the new record into the database
            DB::table('lms_lesson')->insert([
                'batchId' => $batchCode,
                'section_title' => $sectionTitle,
                'lesson_title' => $lessonTitle,
                'link' => $link,
                'lesson_password' => $lessonPassword,
                'lesson_doc' => $newPathsStringEdit,
                'lesson_quiz' => $lessonQuiz,
                'ins_note' => $lessonNote,
            ]);
}
0

There are 0 best solutions below