| Server IP : 13.235.167.51 / Your IP : 216.73.217.116 Web Server : Apache System : Linux machinox-server 6.17.0-1013-aws #13~24.04.1-Ubuntu SMP Fri Apr 24 21:36:58 UTC 2026 aarch64 User : root ( 0) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/app.machinox.in/app/Traits/ |
Upload File : |
<?php
namespace App\Traits;
use Aws\S3\S3Client;
use Carbon\Carbon;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
// use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use PDO;
trait UploadTrait
{
public function uploadOne($uploadedFile, $folder = null, $fileName = null, $expiry = 2880)
{
$disk = config("filesystems.default");
$name = !is_null($fileName) ? $fileName : (Str::random(25) . "." . $uploadedFile->getClientOriginalExtension());
$filePath = $folder . "/" . $name;
if ($disk == 's3') {
if (!Storage::disk('public')->exists($folder)) {
Storage::disk('public')->makeDirectory($folder);
}
Storage::disk("public")->put($filePath, file_get_contents($uploadedFile));
$s3Client = new S3Client([
'region' => config('filesystems.disks.s3.region'),
'version' => 'latest',
]);
$s3Bucket = config('filesystems.disks.s3.bucket');
$s3Key = $folder . "/" . $name; // your file name
$s3Options = [];
$options = [
'Bucket' => $s3Bucket,
'Key' => $s3Key,
'MetaData' => $s3Options,
];
$fileExt = strtolower(pathinfo($filePath, PATHINFO_EXTENSION));
if (in_array($fileExt, ['xlsx', 'pdf', 'png', 'jpg', 'jpeg'])) {
if ($fileExt == 'pdf') {
$s3Options = [
'ContentType' => 'application/pdf',
'Content-Disposition' => 'attachment'
];
}
if ($fileExt == 'xlsx') {
$s3Options = [
'ContentType' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8',
];
}
if ($fileExt == 'png') {
$s3Options = [
'ContentType' => 'image/png',
];
}
if ($fileExt == 'jpg' || $fileExt == 'jpeg') {
$s3Options = [
'ContentType' => 'image/jpeg',
];
}
// Define the custom headers (metadata)
// $s3Options = [
// 'ContentType' => ($fileExt == 'pdf') ? 'application/pdf' : 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8',
// 'CacheControl' => 'max-age=3600', // Set cache expiration
// 'ContentDisposition' => 'inline', // Or 'attachment' for download
// ];
$s3Options['CacheControl'] = 'max-age=3600';
$s3Options['ContentDisposition'] = 'inline';
$options['MetaData'] = $s3Options;
$options['ContentType'] = $s3Options['ContentType'];
$options['SourceFile'] = Storage::disk("public")->path($filePath);
$options['CacheControl'] = $s3Options['CacheControl'];
$options['ContentDisposition'] = $s3Options['ContentDisposition'];
$command = $s3Client->getCommand('PutObject', $options);
// Upload the file to S3
$result = $s3Client->execute($command);
if (Storage::disk("public")->exists($filePath)) {
Storage::disk("public")->delete($filePath);
}
// Return the URL
return $result['ObjectURL'];
}
$command = $s3Client->getCommand('PutObject', $options);
$request = $s3Client->createPresignedRequest($command, now()->addMinutes($expiry));
$presignedUrl = (string) $request->getUri();
return $this->uploadFile($presignedUrl, $uploadedFile);
}
return Storage::disk($disk)->put($filePath, file_get_contents($uploadedFile));
}
private function uploadFile($url, $data)
{
$image = fopen($data->getPathName(), "rb");
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_PUT, 1);
curl_setopt($curl, CURLOPT_UPLOAD, 1);
curl_setopt($curl, CURLOPT_INFILE, $image);
curl_setopt($curl, CURLOPT_INFILESIZE, filesize($data->getPathName()));
$result = curl_exec($curl);
curl_close($curl);
}
public function deleteOne($folder, $filename = null)
{
$disk = config("filesystems.default");
if (!empty($filename)) {
$url = explode("?", $filename);
$filename = basename($url[0]);
Log::info("Delete File Name :" . $filename);
if ($disk == 's3') {
if ($this->fileExists($folder, $filename)) {
$s3Client = new S3Client([
'region' => config('filesystems.disks.s3.region'),
'version' => 'latest',
]);
$s3Bucket = config('filesystems.disks.s3.bucket');
$s3Key = $folder . "/" . $filename; // your file name
$s3Client->deleteObject(
array(
'Bucket' => $s3Bucket,
'Key' => $s3Key,
)
);
}
} else {
if ($this->fileExists($folder, $filename)) {
Storage::disk($disk)->delete($folder . '/' . $filename);
}
}
}
}
public function getURL($folder, $filename = null, $expiry = 2880)
{
$disk = config("filesystems.default");
if (!empty($filename)) {
if ($this->fileExists($folder, $filename)) {
if ($disk == "public") {
return url($folder . '/' . $filename);
}
if ($disk == 's3') {
return Storage::disk($disk)->temporaryUrl($folder . "/" . $filename, now()->addMinutes($expiry));
}
return Storage::disk($disk)->url($folder . '/' . $filename);
}
}
return null;
}
public function fileExists($folder, $filename = null)
{
$disk = config("filesystems.default");
if (!empty($filename)) {
$file = basename(parse_url($filename, PHP_URL_PATH));
if (Storage::disk($disk)->exists($folder . '/' . $file)) {
return true;
}
}
return false;
}
}