fix: resolve Class finfo not found on PO receipt upload when PHP fileinfo extension is missing
Some checks failed
Tests / PHP 8.3 (push) Has been cancelled
Tests / PHP 8.4 (push) Has been cancelled
Tests / PHP 8.5 (push) Has been cancelled

This commit is contained in:
2026-05-26 14:17:54 +08:00
parent cb2caea951
commit e6a9a80c32
3 changed files with 185 additions and 50 deletions

View File

@@ -334,14 +334,37 @@ class PurchaseOrderController extends Controller
return back()->with('error', 'This purchase order is already paid.');
}
$validated = $request->validate([
'receipt' => 'required|file|mimes:pdf,jpg,jpeg,png|max:10240',
$hasFileInfo = extension_loaded('fileinfo');
$rules = [
'receipt' => 'required|file|max:10240',
'warehouse_ulid' => 'nullable|string',
]);
];
if ($hasFileInfo) {
$rules['receipt'] .= '|mimes:pdf,jpg,jpeg,png';
}
$validated = $request->validate($rules);
$file = $request->file('receipt');
if (!$hasFileInfo) {
$extension = strtolower($file->getClientOriginalExtension() ?: pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION));
if (!in_array($extension, ['pdf', 'jpg', 'jpeg', 'png'])) {
return back()->withErrors(['receipt' => 'The receipt must be a file of type: pdf, jpg, jpeg, png.']);
}
}
// Store receipt file
$file = $request->file('receipt');
$path = $file->store("po-receipts/{$purchaseOrder->id}", 'public');
if ($hasFileInfo) {
$path = $file->store("po-receipts/{$purchaseOrder->id}", 'public');
} else {
$extension = strtolower($file->getClientOriginalExtension() ?: pathinfo($file->getClientOriginalName(), PATHINFO_EXTENSION));
$fileName = \Illuminate\Support\Str::random(40) . '.' . $extension;
$path = "po-receipts/{$purchaseOrder->id}/" . $fileName;
\Illuminate\Support\Facades\Storage::disk('public')->put($path, fopen($file->getRealPath(), 'r'));
}
DB::transaction(function () use ($purchaseOrder, $path, $file, $request) {
$purchaseOrder->update([