Ngozistephen's avatar

File Upload and Registration

I am trying to upload a files and store them in the vendor table, after delete the folder from the Temporary table. but i keep getting this error message "message": "File C:\\laragon\\www\\umojabackend\\storage\\app/public/business_image/tmp/6617fb1805c1f-1712847640/ does not exist",

this is my code


class VendorRegisterController extends Controller
{

    public function register(VendorRegistrationRequest $request){
    
        $role = Role::where('name', 'Vendor')->value('id');

        $vendor = Vendor::create([
            'first_name' => $request->first_name,
            'last_name' => $request->last_name,
            'email' => $request->email,
            // 'password' => Hash::make($request->password),
            'language' => $request->language,
            'rep_country' => $request->rep_country,
            'gender' => $request->gender,
            'date_birth' => $request->date_birth,
            'country_name' => $request->country_name,
            'vendor_id_form_type' => $request->vendor_id_form_type,
            'vendor_id_number' => $request->vendor_id_number,
            'phone_number' => $request->phone_number,
            'company' => $request->company,
            'address' => $request->address,
            'apartment_suite' => $request->apartment_suite,
            'state' => $request->state,
            'city' => $request->city,
            'business_name' => $request->business_name,
            'business_type' => $request->business_type,
            'business_website' => $request->business_website,
            'business_number' => $request->business_number,
            'tax_id_number' => $request->tax_id_number,
            'utility_bill_type' => $request->utility_bill_type,
            'role_id' => $role, 
        ]);

        $fileTypes = $this->getFileTypes();
        foreach ($fileTypes as $fileType) {
            if ($request->has($fileType)) {
                $folderName = $request->input($fileType); // Retrieve folder name from request
        
                $tempFile = TemporaryFile::where('folder', $folderName)->first();
                
                
                if ($tempFile) {
                    $mediaPath = storage_path("app/public/{$fileType}/tmp/{$folderName}/{$tempFile->fileName}");
                    if (file_exists($mediaPath)) {
                        $vendor->addMedia($mediaPath)->toMediaCollection($fileType);
    
                        // Properly delete the directory
                        $deleted = Storage::deleteDirectory("public/{$fileType}/tmp/{$folderName}");
    
                        if (!$deleted) {
                            \Log::error("Failed to delete directory: public/{$fileType}/tmp/{$folderName}");

                            return response()->json(['error' => 'Failed to delete files'], 500);
                        }
    
                        $tempFile->delete();
                    } else {
                        \Log::error("File not found: {$mediaPath}");

                        // Return a response indicating failure
                        return response()->json(['error' => 'File not found'], 404);
                    }

                    
                }
            }
        }
   

        
       
        
        event(new Registered($vendor));

        $device = substr($request->userAgent() ?? '', 0, 255);

        $token = $vendor->createToken($device)->accessToken;

        $response = [
            'access_token' => $token,
            'user' => $vendor->first_name,
            'role' => Role::find($role)->name,
            'Message' => 'registered successfully.'
        ];

        return response()->json($response,  Response::HTTP_CREATED);
    }

  

    public function upload(Request $request)
    {
        $uploadedFolders = [];

        $fileTypes = $this->getFileTypes();

        foreach ($fileTypes as $fileType) {
            if ($request->hasFile($fileType)) {
                $file = $request->file($fileType);
                $fileName = $file->getClientOriginalName();
                $folder = uniqid() . '-' . now()->timestamp;

               
                $file->storeAs("/$fileType/tmp/" . $folder, $fileName);

                TemporaryFile::create([
                    'folder' => $folder,
                    'file_name' => $fileName,
                    'fileType' => $fileType 
                ]);

                $uploadedFolders[$fileType] = $folder;
            }
        }

        return $uploadedFolders;
    }

    private function getFileTypes()
    {
        return ['business_image', 'profile_photo', 'picture_vendor_id_number', 'utility_photo', 'business_number_photo'];
    }

  


}

while the file exist

0 likes
2 replies
amitsolanki24_'s avatar

@ngozistephen please check this file exists on a mention path or not.

I think error is coming from this line

$deleted = Storage::deleteDirectory("public/{$fileType}/tmp/{$folderName}");

Snapey's avatar

confirm what line of code the error occurs on

Please or to participate in this conversation.