Store blob as a file in S3 with Laravel - php

I am using a file upload plugin in Vue to upload some image and pdf files through an API and it has an option to create a blob field when uploading the files.
The request sent to Laravel is as follows. I can access the blob from the browser by copying and pasting the URL on the browser.
On the Server side code, I am trying to save the file with the following code but the saved file is some corrupted 64byte file rather than the actual image. How would we store the blob as a normal file in the filesystem?
if ($request->has('files')) {
$files = $request->get('files');
$urls = [];
foreach ($files as $file) {
$filename = 'files/' . $file['name'];
// Upload File to s3
Storage::disk('s3')->put($filename, $file['blob']);
Storage::disk('s3')->setVisibility($filename, 'public');
$url = Storage::disk('s3')->url($filename);
$urls[] = $url;
}
return response()->json(['urls' => $urls]);
}

Try by replacing this:
Storage::disk('s3')->put($filename, $file['blob']);
with this:
Storage::disk('s3')->put($filename, base64_decode($file['blob']));

Related

how to not generate hash when uploading image files to ftp server with laravel?

so I have successfully uploaded the image file to the ftp server and the image file is shaped like an auto generated hash file name, so how do I make the file name I upload is the same as the image name?, because when it is saved to the database it is not hashed but the name the file is the same as the uploaded image, but on the ftp server when saving it the file name is shaped like a hash
example upload file in ftp server :
example controller :
$lampiran = Lampiran_tte::where('surat_tte_id', $surat->id);
if ($request->hasfile('lampiran_gambar')) {
$files = [];
foreach ($request->file('lampiran_gambar') as $file) {
if ($file->isValid()) {
$filename = $file->getClientOriginalName();
$file->store('/lampiranSurat', 'ftp') . '/' . $filename;
$files[] = [
'lampiran_gambar' => $filename,
];
}
}
$gambar = '';
foreach ($files as $value) {
$gambar .= $value['lampiran_gambar'].'#';
}
$gambar = substr($gambar, 0, -1);
$lampiran->update([
'isi_lampiran' => $request->isi_lampiran,
'lampiran_gambar' => $gambar,
'update_by' => Auth::user()->name,
]);
}
if it is already stored in the database,
the image file name matches the image file name that was uploaded
example in database :
The store() function is generating the unique ID value for the file. Use the storeAs() function instead which allows you to specify a filename of your choice.
$file->storeAs('/lampiranSurat', $filename, 'ftp');

Laravel Cant save image using Storage

I cant save a image using the storage method.
If I do: $file->move(public_path().'/', $file->getClientOriginalName());
The image will get saved and I can open it in finder.
But if I save the image using:
$extension = $file->guessExtension();
$filename = 'thumnail_'.$id.'.'.$extension;
if ($file) {
$s3 = Storage::disk('public')->put($filename, $file);
}
It will also get saved to the folder but then I cant open it because the file is corrupted
And to be clear "file" is sent to my model from a controller: $file = $request->file('images');

Get image from local path in php

I am working on a bulk upload project using excel sheet and using CakePHP 3.2 for writing application.
I have an excel sheet with a column to give image to be uploaded.
User have 3 choices to go with either
Upload images to a pre defined directory before bulk upload and give the name of the image in the cell and image will be automatically
selected from path.
Give the url of the image (http://website/path/image.jpg)
Give the path of the image if it is on local machine. Ex., C:\user\pictures\image.jpg if windows, and
/home/user/picture/image.jpg if linux
This is what I'm doing to save images
$p_image = $objWorksheet->getCellByColumnAndRow(30, $row)->getValue();
if (filter_var($p_image, FILTER_VALIDATE_URL)) {
// get image from url
$full_image_path = $p_image;
} else {
// get image from folder
$path = Configure::read('media.bulkUpload.pre.product');
// full path of the image from root directory
$full_image_path = $path . DS . $p_image;
}
$upload_path = Configure::read('media.upload') . DS . 'files' . DS;
// new name of image
$img_new_name = uniqid('img_').round(microtime(true) * 1000).rand(1,100000);
if ($full_image_path) {
// generate uuid directory name
$dir = Text::uuid();
// create new directory
mkdir($upload_path.$dir, 0777, true);
// save file
try {
$img = new \abeautifulsite\SimpleImage($full_image_path);
// save image of original size
$img->best_fit(850,1036)->save($upload_path.$dir.'/'.$img_new_name.'.jpg');
} catch(Exception $e) {
echo 'Error: '.$e->getMessage();
}
}
Image upload using url and pre ftp upload is working fine. But, how could I get image from the path of local system and then save them.
You can't do this without the users uploading the image themselves. You cannot retrieve a file from the client's filesystem from an external server where you run your php.

Uploading a base64 image as an image file

So I'm receiving an image in base64 encoding. I want to decode the image and upload it in a specific directory. The encoded file is an image and I'm using $file = base64_decode($base64_string); to decode it. Uploading it via file_put_contents('/uploads/images/', $file); always results in failed to open stream: No such file or directory error.
So, how do I take the decoded string, and upload it on a specific path?
Thanks.
Your path is wrong.
/uploads/images
is checking for a folder in the / directory called uploads. You should specify the full path to the folder:
/var/www/vhosts/MYSITE/uploads/images
I had the same problem and finally solved using this:
<?php
$file = base64_decode($base);
$photo = imagecreatefromstring($file);
//create a random name
$RandomStr = md5(microtime());
$name = substr($RandomStr, 0, 3);
$name .= date('Y-m-d');
$name .= '.jpg';
//assign name and upload your image
imagejpeg($photo, 'images/'.$name, 100);

dynamicaly creating file ,file path not working on server machine

I am saving canvas to a file. Code creates a png file in the upload folder. It is working correctly on local machine but when i try to run this on the server, I am not able to find the file in the upload folder. am i giving wrong path ?
After file creation i am printing alert, so i get file creation alert but file is not just created in the upload folder .
if ( isset($_POST["image"]) && !empty($_POST["image"]) ) {
// get the image data
$data = $_POST['image'];
list($type, $data) = explode(';', $data);
list(, $data) = explode(',', $data);
$data = base64_decode($data);
//Image name
$filename ="image". md5(uniqid()) . '.png';
$file ='../upload/'.$filename;
// decode the image data and save it to file
file_put_contents($file,$data);
}
make sure directory '../upload' exists or create it first

Categories