1

making an api to upload multifile with an audio everything is working but audio file can't uploaded and uploading with dd($request)->all then it works but while uploading with any condition its gives null value on every clientoriginalName ,extension, how t fix this...

<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use App\File;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Auth;


class FileController extends Controller
{
      public function store(Request $request)
    {
        $this->validate($request, [
            'title' => 'required',
            'description' => 'nullable',
            'file' => 'required|file|mimes:' . File::getAllExtensions() . '|max:' . File::getMaxSize(),
            'Fileaudio' =>'nullable|mimes:audio/mpeg,mpga,mp3,wav,aac'
        ]);
////////////      All files       //////////////////
   $file = new File();
  $title = $request->title;
  $uploaded_file = $request->file('file');
  $filename = $uploaded_file->getClientOriginalName();
  $original_ext = $uploaded_file->getClientOriginalExtension();
  $type = $file->getType($original_ext);
  $filepath = $uploaded_file->storeAs('public/upload/files/',$filename);
  $files = URL::asset('storage/upload/files/' . $filename);
  $description = $request->description;
  $user_id = Auth::user()->id;
/////////// Audio at null    /////////////////
     $Fileaudio = $request->file('audio');
     $audioname = $Fileaudio->getClientOriginalName();
     $audiopath =$Fileaudio->storeAs('public/upload/files/audio/', $audioname);

  //return $audiopath;
   dd($request->all());

 }

}

and i am sending request to postman...

13
  • Are you sending multi-files requests? Commented Mar 11, 2019 at 14:04
  • yes multipart/form data but not in a single request Commented Mar 11, 2019 at 14:09
  • link using postman Commented Mar 11, 2019 at 14:13
  • am i doing something fishy pls tell ... Commented Mar 11, 2019 at 14:15
  • 1
    From your postman request I see FileAudio and in your controller I see $Fileaudio = $request->file('audio'); instedad of $Fileaudio = $request->file('FileAudio'); Commented Mar 11, 2019 at 14:34

3 Answers 3

5

create a folder 'upload/files' inside storage/app/public , and /upload/files/audio

then run command : php artisan storage:link

this command will link your storage folder to public folder

update your code :

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Storage;
use App\File;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;

public function store(Request $request)
    {
        // validation
        $this->validate($request, [
            'title' => 'required',
            'description' => 'nullable',
            'file' => 'required|file|mimes:jpeg,jpg,png,gif|max:2048',
            'audio' =>'nullable|file|mimes:audio/mpeg,mpga,mp3,wav,aac'
        ]); 

         // code for upload 'file'

          if($request->hasFile('file')){
            $uniqueid=uniqid();
            $original_name=$request->file('file')->getClientOriginalName();
            $size=$request->file('file')->getSize();
            $extension=$request->file('file')->getClientOriginalExtension();
            $name=Carbon::now()->format('Ymd').'_'.$uniqueid.'.'.$extension;
            $imagepath=url('/storage/uploads/files/'.$name);
            $path=$request->file('file')->storeAs('public/uploads/files/',$name);  
           }


        // code for upload 'audio'
        // handle multiple files 
       if(is_array($request->file('audio')))
        {
         $audios=array();
         foreach($request->file('audio') as $file) {
            $uniqueid=uniqid();
            $original_name=$file->getClientOriginalName();
            $size=$file->getSize();
            $extension=$file->getClientOriginalExtension();
            $filename=Carbon::now()->format('Ymd').'_'.$uniqueid.'.'.$extension;
            $audiopath=url('/storage/upload/files/audio/'.$filename);
            $path=$file->storeAs('/upload/files/audio',$filename);
            array_push($audios,$audiopath);
         }
         $all_audios=implode(",",$audios);
     }else{ 
         // handle single file 
         if($request->hasFile('audio')){
         $uniqueid=uniqid();
         $original_name=$request->file('audio')->getClientOriginalName();
         $size=$request->file('audio')->getSize();
         $extension=$request->file('audio')->getClientOriginalExtension();
         $filename=Carbon::now()->format('Ymd').'_'.$uniqueid.'.'.$extension;
         $audiopath=url('/storage/upload/files/audio/'.$filename);
         $path=$file->storeAs('public/upload/files/audio/',$filename);
         $all_audios=$audiopath;
        }
    }

}

in your postman request :

add key : "file"  for image file , 
          "audio" for audio file 
Sign up to request clarification or add additional context in comments.

5 Comments

for single request it shows error "message": "Call to a member function getClientOriginalName() on null"
just making a dd request for multi then it works bt when for single request then errors are same
@Aoudeshkumar , make sure you are passing multipart header in request
added if condition ,check
"message": "Undefined variable: file", now on single request it shows error eception
0

you can use these there sentences for upload any multipart

$file = $request->file;
$filename = time() . '.' . $file->getClientOriginalExtension();
$file->move('your-path', $filename);

and if you need to upload multi audios or images make sure your key on postman wrote like this

images[] 
or
audios[]

Comments

0
$file = new File();
    $title = $request->title;
    $uploaded_file = $request->file('file');
    $filename = $uploaded_file->getClientOriginalName();
    $original_ext = $uploaded_file->getClientOriginalExtension();
    $type = $file->getType($original_ext);
    $filepath = $uploaded_file->storeAs('public/upload/files/', $filename);
    $files = URL::asset('storage/upload/files/' . $filename);
    $description = $request->description;
    $user_id = Auth::user()->id;

    $Fileaudio = $request->file('Fileaudio'); // Use 'Fileaudio' consistently
    if ($Fileaudio) {
        $audioname = $Fileaudio->getClientOriginalName();
        $audiopath = $Fileaudio->storeAs('public/upload/files/audio/', $audioname);
    } else {
        $audiopath = null;
    }

    dd($request->all(), $filename, $audioname ?? 'No audio uploaded');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.