JOHNMAC's avatar

file isn't replacing on edit in laravel

hi m trying to edit the files in Laravel, but when I edit the file then its name is changing according to edit in db but file isn't replacing the previous one in files folder, m using resource route for it,,,,,,,,,,,,,,,,,Note: and when I store file then its name stores in one table and its data stores in another table, the need is that when I edit the file then its data from the other file must changes, how to do it?

controller:

        <?php

         namespace App\Http\Controllers;

         use App\File;
         use Illuminate\Http\Request;
         use Spatie\PdfToText\Pdf;
         use Illuminate\Support\Facades\File as FileFacade;
         use DB;

         class FileController extends Controller
         {

             public function store(Request $request)
             {
                 request()->validate([
                'filename' => 'required',
                 ]);

              $cols = ['Battery', 'No_of_questions_attempted', 'SAS', 'NPR', 'ST', 'GR'];

              $files = $request->file('filename');
              foreach ($files as $file) {
                  $file_id = File::create([
                      'filename' => $file->getClientOriginalName(),
                  ])->id;

                $text = Pdf::getText($file->getRealPath(), base_path('poppler-utils/bin/pdftotext.exe'));

                $page5 = strpos($text, 'Page 5 of 8');
                $start = strpos($text, 'Verbal', $page5);
                $end = strpos($text, "\r\n\r\nSAS (with 90% confidence bands)", $page5);

                $table = substr($text, $start, $end - $start);

                $data = array_map(function ($row) use ($cols, $file_id) {
                    return array_merge(compact('file_id'), array_combine($cols, $row));
                }, collect(explode("\r\n\r\n", $table))->chunk(count($cols))->toArray());

                \DB::table('importpdfs')->insert($data);

                $file->move(public_path('files'), $file->getClientOriginalName());
              }

            return redirect('/file')->with('Success', 'File uploaded successfully');
          }

           public function edit($id)
           {
              $data = File::findOrFail($id);
              $files = \DB::table('files')->get();

              return view('edit', compact('data', 'files'));
           }

           public function update(Request $request, $id)
           {

               $request->validate([
                  'filename' => 'required',
               ]);

               $file = $request->file('filename');
               $clientName = $file->getClientOriginalName();
               $path = $file->move(public_path('files'), $clientName);
               File::whereId($id)->update(['filename' => $clientName]);
             return redirect('file')->with('success', 'Data is successfully updated');
        }

       }
        

blade file:

      <form method="post" action="{{ route('file.update', $data->id) }}" enctype="multipart/form-data">
        @csrf
        @method('PATCH')
      <div class="form-group">
        <label for="EditFile">Edit File</label>
        <input type="file" name="filename" class="form-control-file">
        <img src="{{ URL::to('/') }}/files/{{ $data->file }}" width="100"/>
        <img src="./images/pdf.png" class="img-circle elevation-2" alt="Pdf Image" width="50">
        <input type="hidden" name="hidden_file" value="{{ $data->file }}" />
        </div>
        <br>
        <input type="submit" name="edit" class="btn btn-primary input-lg" value="Edit" />
    </form>
0 likes
0 replies

Please or to participate in this conversation.