skip to Main Content

hi i have a contract Controller when i can upload the audio file foreach contract and it works very good in local but when i upload it to the server it wont work when i upload a mp3 file i can not see him upload like the page just refresh without uploading the file please help me

ContractController

  public function upload(Request $request, $id)
    {
        $request->validate([
            'enregistrements.*' => 'required|mimes:mp3|max:10240',
        ]);
    
        $contract = Contract::findOrFail($id);
    
       
        foreach ($request->file('enregistrements') as $file) {
            // Get original file name and sanitize it
            $originalName = $file->getClientOriginalName();
            $sanitizedFileName = preg_replace('/[^A-Za-z0-9-.]/', '_', $originalName);
            
            // Store the file with sanitized name in 'public/enregistrements' directory
            $filePath = $file->storeAs('enregistrements', $sanitizedFileName, 'public');
            
            // Save the file path in the database
            AudioRecording::create([
                'contract_id' => $contract->id,
                'file_path' => $filePath,
            ]);
        }
        
    
        return redirect()->route('contracts.index')->with('success', 'Enregistrements uploadés avec succès.');
    }

Upload Modal

<!-- Upload Modal -->
@foreach ($contracts as $contract)
<div class="modal fade profileModal-modal" id="profileModal" tabindex="-1" role="dialog" aria-labelledby="profileModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
      <div class="modal-content">

        <div class="modal-header" id="profileModalLabel">
            <h5 class="modal-title">Ajouter <b>Enregistrements</b></h5>
            <button type="button" class="btn-close" data-bs-dismiss="modal" aria-hidden="true">
                <svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-x">
                    <line x1="18" y1="6" x2="6" y2="18"></line>
                    <line x1="6" y1="6" x2="18" y2="18"></line>
                </svg>
            </button>
        </div>
        <div class="modal-body">
            <form id="uploadForm" action="{{ route('contracts.upload', $contract->id) }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="mb-3">
                    <input class="form-control file-upload-input" type="file" id="formFile" name="enregistrements[]" accept=".mp3" multiple required>
                </div>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-light-danger mt-2 mb-2 btn-no-effect" data-bs-dismiss="modal">Annuler</button>
            <button type="submit" class="btn btn-primary mt-2 mb-2 btn-no-effect">Upload</button>
        </div>
        </form>
      </div>
    </div>
</div>
@endforeach

2

Answers


  1. Add a try catch and log the exception, you will find the issue, most probably it’s because of the application_url value mismatch in ENV

    try something like this

     use IlluminateSupportFacadesLog; 
     try {
            $request->validate([
                'enregistrements.*' => 'required|mimes:mp3|max:10240',
            ]);
    
            $contract = Contract::findOrFail($id);
    
    
            foreach ($request->file('enregistrements') as $file) {
                // Get original file name and sanitize it
                $originalName = $file->getClientOriginalName();
                $sanitizedFileName = preg_replace('/[^A-Za-z0-9-.]/', '_', $originalName);
    
                // Store the file with sanitized name in 'public/enregistrements' directory
                $filePath = $file->storeAs('enregistrements', $sanitizedFileName, 'public');
    
                // Save the file path in the database
                AudioRecording::create([
                    'contract_id' => $contract->id,
                    'file_path' => $filePath,
                ]);
            }
        }catch (Throwable $th) {
            Log::info($th->getMessage());
        }
    

    and check your env

    APP_URL=https://example.com //provide your domain here
    
    Login or Signup to reply.
  2. It seems like there is file permission issue. You can check the file permission by using ls -l enregistrements. check whether you have write permission or not. you can give permission using sudo chmod 755 path/of/folder.
    other than this you can add try catch block to get the errors.

    public function upload(Request $request, $id)
    {
        try {
            $request->validate([
                'enregistrements.*' => 'required|mimes:mp3|max:10240',
            ]);
    
            $contract = Contract::findOrFail($id);
    
    
            foreach ($request->file('enregistrements') as $file) {
                // Get original file name and sanitize it
                $originalName = $file->getClientOriginalName();
                $sanitizedFileName = preg_replace('/[^A-Za-z0-9-.]/', '_', $originalName);
    
                // Store the file with sanitized name in 'public/enregistrements' directory
                $filePath = $file->storeAs('enregistrements', $sanitizedFileName, 'public');
    
                // Save the file path in the database
                AudioRecording::create([
                    'contract_id' => $contract->id,
                    'file_path' => $filePath,
                ]);
            }
    
    
            return redirect()->route('contracts.index')->with('success', 'Enregistrements uploadés avec succès.');
        } catch (Exception $e) {
            return redirect()->route('contracts.index')->with('error', 'Error'. $e);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search