Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fatima1's avatar

uploaded multiple file function getClientOriginalName() on null

hi all i'm working with laravel 5.2 i have multiple uploaded file

First: if($request->hasfile('filename')) {

        foreach ($request->file('filename') as $file) {
            $name1= $file->getClientOriginalName();
             $name = time().$name1;
            //  $file->move('files', $name);
               $data[] = $name;
        }
        return $data;
    }

output: Call to a member function getClientOriginalName() on null

Second: if($request->hasfile('filename')) {

        foreach ($request->file('filename') as $file) {
            $name1= $file->getClientOriginalName();
             $name = time().$name1;
            //  $file->move('files', $name);
               $data[] = $name;

return $data; }

    }

output: ["1544184628abcdef.pdf"]

i wand the second output with the first way, how to solve this problem??

0 likes
23 replies
Talinon's avatar

The only difference between your two code samples is that the second returns on the first iteration of your foreach loop. This suggests within the first sample that there is something within your array that is not a file object, or maybe you have a nested array.

Could you paste your form code?

The first thing I'd check is to make sure you have enctype="multipart/form-data" within your tag.

fatima1's avatar

@TALINON - {!! Form::open(['method'=>'POST','action'=>'CareerController@store','files'=> true]) !!}

                                                <div >
                                                    <label id="fileT" class="choose btn  btn-default form-control {{($errors->first('filename') || $errors->first('filename.mimes') ? " error " : " ")}}" >Choose File
                                                        <input type="file"  id="f1"  name="filename[]" onchange="showname()" style="line-height: inherit;margin-top: 4%;">
                                                    </label>
                                                </div>

                                                <p id="file-name" class=" nameF" style="display:block;color: black;font-size: 14px;"></p>

                                                 <button class="btn btn-danger pull-right fa fa-trash" type="button" id="file-reset" style="margin-left: auto;border-radius: 24px;"></button>



                                                </div>

{{Form::close()}}

public function store(CareerRequest $request) { $job = Job::findOrFail($request->job_id);

    if($request->hasfile('filename'))
    {

        foreach($request->file('filename') as $file)
        {
           $name1= $file->getClientOriginalName();
            $name = time().$name1;
            $file->move('/Applications/XAMPP/xamppfiles/htdocs/careers1/storage/app/public/files', $name);
            $data[] = $name;


        }

    }
    $i=0;
    $jr=  JobReq::create([
        'job_id'=>$job->id,
        'name'=>$request->name,
        'email'=>$request->email,
        'title'=> $job->title,
        'message'=>$request->message,
        'order'=>$i,
        'rand_url' => rand(0000,9999),

    ]);

    $jr->update(['order' => 1]);

    $file = File::create([
        'filename'=>json_encode($data),
        'jobReq_id'=>$jr->id
    ]);
   
 return back();

}

it was working in laravel 5.6 but i downgrade to 5.2 then this error comes

Talinon's avatar

If you want to select multiple files for upload, you need to add the multiple attribute to your input tag:

<input type="file"  id="f1"  name="filename[]" onchange="showname()" style="line-height: inherit;margin-top: 4%;" multiple>

If that doesn't solve the problem, what is the content of dd($request->file('filename')) within your controller's store method?

fatima1's avatar

@TALINON - dd($request->file('filename'))

it return null

but if i did dd($request->all())

it returns the filename

fatima1's avatar

@TALINON - array:6 [▼ "_token" => "70NoGq6zA1qKWmAlZ209I6DCoiZE9fmR6v1nkJ8p" "filename" => array:3 [▼ 0 => "IMG_2178.jpeg" 1 => "cv.pdf" 2 => "" ] ]

amk's avatar

Try with this!

if($request->hasFile('filename')){
 dd($request->file('filename');
 }
fatima1's avatar

@AMK - array:3 [▼ 0 => UploadedFile {#168 ▶} 1 => null 2 => null ]

it returns the file name but it return nothing when dd($file) outside the if conditions

amk's avatar

pls try with only one photo not multiple! Just test it! And,how is output?

<input type="file  name="filename">
Talinon's avatar

You should be getting an array of UploadedFile objects. It's not immediately clear to me why that might be. Someone else might have an idea. If something comes to mind, I'll let you know.

fatima1's avatar

@AMK - yes i test it, it retrurn the right filename

1 like
amk's avatar

I think your problem is js!

onchange="showname()"

You changed your input type 'file' to 'text' with js! Remove your js on input type file. and test with this

<input type="file  name="filename[]" multiple>

Updated!

fatima1's avatar

@AMK - yes i checked, i think because of that. i will try to change it

amk's avatar

If my answer is correct,pls can you choose best answer?

Snapey's avatar

glad if you sorted it.

please learn to format code properly in your questions

1 like
fatima1's avatar

@SNAPEY - how to make it look good, i try but didn't find how to do it

fatima1's avatar

@AMK - its actually not because of js thing. I have three input file if i upload only one or two files it give this error BUT when i upload three files it gives the right output.

Snapey's avatar

format code by putting three backticks ``` on its own line before and after each block

(see the instructions at the bottom of the comment box when posting)

amk's avatar

So,What is your problem? you should show us all code!

fatima1's avatar
fatima1
OP
Best Answer
Level 1

@AMK - its work finally. what i have done before is that i was putting foreach() for a the three file input.

 if($request->hasfile('filename'))
        {

            foreach($request->file('filename') as $file)
            {
                $name1=$file->getClientOriginalName();
                $name = time().$name1;
            }
        }

NOW , i m ding each file input alone

 if($request->hasfile('filename1')) {
            $file1 = $request->file('filename1');
            $name1 = time() . $file1->getClientOriginalName();
          
        }

 if($request->hasfile('filename2')) {
            $file2 = $request->file('filename2');
            $name2 = time() . $file2->getClientOriginalName();
        }

 if($request->hasfile('filename3')) {
            $file3 = $request->file('filename3');
            $name3 = time() . $file3->getClientOriginalName();
          
        }

Talinon's avatar

I guess you'll be back the first time a user tries to upload 4 or more files.

fatima1's avatar

@TALINON - I hope not. Because I specify that the user can upload one to 3 files only

Please or to participate in this conversation.