Files
portal-api/app/Http/Services/Upload.php
T
2025-07-24 16:18:27 +07:00

81 lines
2.1 KiB
PHP

<?php
namespace App\Http\Services;
use Illuminate\Support\Facades\Storage;
class Upload
{
protected $path = 'public';
protected $fileSize = 1024000;
protected $mineTypeFile = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel','text/csv'];
protected $mineType = ['image/jpeg','image/png','image/webp','image/svg+xml'];
public function ImageUpload($file = null)
{
if (! $file)
{
return false;
}
$fileName = $file->getClientoriginalName();
$fileType = $file->getClientmimeType();
if (! in_array($fileType,$this->mineType))
{
return [
'error' => true,
'message' => 'File không hợp lệ'
];
}
if ($file->getSize() > $this->fileSize)
{
return [
'error' => true,
'message' => 'File vượt kích thước cho phép'
];
}
if (Storage::exists('Upload/'. date('Y/m/d') . '/'.$fileName)) {
$fileName = time() . "_" .$fileName;
}
Storage::putFileAs('Upload/'. date('Y/m/d') . '/',$file,$fileName);
if (Storage::exists('Upload/'. date('Y/m/d') . '/'.$fileName))
{
return [
'error' => false,
'data' => 'Upload/'. date('Y/m/d') . '/'.$fileName,
'filename' => $fileName
];
}
return [
'error' => true,
'message' => 'File upload không thành công. Vui lòng thử lại!'
];
}
public function FileUpload($file = null)
{
if (! $file)
{
return false;
}
$fileName = $file->getClientoriginalName();
$fileType = $file->getClientmimeType();
if (! in_array($fileType,$this->mineTypeFile))
{
return false;
}
Storage::putFileAs('Excel/',$file,$fileName);
if (Storage::exists('Excel/'.$fileName))
{
return 'Excel/'.$fileName;
}
return false;
}
}