how to solve file not uploaded due to an unknown error in laravel 6 when uploading the same file to multiple locations?

  file, file-upload, laravel, laravel-storage

in my application, I am trying to upload one same file to two different paths inside a foreach loop. but it uploads the file to the first location and when uploading to the other it throws the below error.

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

below is my controller function:

public fileUpload(Request $request)
{
    try{
            $ids = explode(',',$request->ids);

            foreach($ids as $key=>$id)
            {
                $program = Program::find($id);
            

                foreach($request->type as $type)
                {
                    if($type == 1)
                    {
    
                        $program_memo = MemoMaster::create([
                            'user_id' => auth()->user()->id,
                            'type_id' => 1,
                            'company_type' => 'C',
                            'sending_type' => 4,
                            'role_id' => 4,
                        ]);
    
                        if($request->hasFile('file')) $this->uploadWishFiles($program_memo ->id, $request->file('file'));
                    
                    }
                    else
                    {
    
                        $program_memo = MemoMaster::create([
                            'user_id' => auth()->user()->id,
                            'type_id' => 1,
                            'company_type' => 'C',
                            'sending_type' => 5,
                            'role_id' => 4,
                        ]);

                        if($request->hasFile('file')) $this->uploadWishFiles($program_memo ->id,  $request->file('file'));
    
                    }
                }
            }

            return true;

        }catch (Throwable $th) {
            return response()->json(['error' => true, 'message' => $th->getMessage()], 500); 
        }
}

uploadWishFile function

private function uploadWishFiles($id, $file)
    {
        $name = str_replace(' ', '-', strtolower($file->getClientOriginalName()));
        $time = time(); //returns the current time as a Unix timestamp
        $final_name = $time . $name;

        $path = storage_path() . '/documents/'.$id.'/';

        if(File::exists($path)) File::deleteDirectory($path);

        try {
            $file->move($path,  $final_name); //error thrown line
        } catch (Exception $ex) {
           $previous_memo = MemoMaster::find(($id-1));
           $from_path = storage_path().'/documents/'.$previous_memo->id.'/'.$previous_memo->file_name;
           $to_path = storage_path().'/documents/'.$id.'/'.$previous_memo->file_name;

            if(!File::exists($to_path)) {
                File::makeDirectory(storage_path().'/documents/'.$id.'/', 0777, true);
                File::copy($from_path , $to_path);
            }

            $final_name = $previous_memo->file_name;
        }

        return true;
    }

when it saves the records to multiple ids, for example, id 1 and 2. MemoMaster table will save a record for these and the memo master id and file sent to uploadWishFile function. This uploads the file successfully to id 1 and when storing the same file to id 2 it throws the above error.

how can I solve this?

Source: Laravel

Leave a Reply