Laravel: Array to string conversion while uploading .sql files - php

Edit: It turns out this issue happens while trying to upload .sql files. It's not the file name.
When I try to upload a file with this name: forge_2016-02-08_--USERS THOUGH.sql I'm shown this error below:
ErrorException in FileinfoMimeTypeGuesser.php line 69:
Array to string conversion
and
at HandleExceptions->handleError('8', 'Array to string conversion', '/home/forge/example.com/vendor/symfony/http-foundation/File/MimeType/FileinfoMimeTypeGuesser.php', '69', array('path' => '/tmp/phppkDGK8', 'finfo' => object(finfo)))
at finfo->file('/tmp/phppkDGK8') in FileinfoMimeTypeGuesser.php line 69
at finfo->file('/tmp/phppkDGK8') in FileinfoMimeTypeGuesser.php line 69
at FileinfoMimeTypeGuesser->guess('/tmp/phppkDGK8') in MimeTypeGuesser.php line 139
I have no idea why this error is happening. Here's my upload code:
$baseDir = storage_path('uploads');
$file = $request->file('file');
$mimeType = $file->getMimeType();
$name = str_random(6) . time() . '-' . str_replace(' ', '_', Str::ascii($file->getClientOriginalName()));
$file->move($baseDir, $name);
$path = $baseDir . '/' . $name;
$data = ['path' => $path, 'ip' => userIP(), 'name' => $file->getClientOriginalName(), 'mime' => $mimeType, 'size' => $file->getClientSize()];
$status = Uploads::create($data);
if ($status) {
$su = true;
Please help guys. I don't know why this is happening.

I fixed it by changing
$mimeType = $file->getMimeType();
to
$mimeType = $file->getClientMimeType();
This fixed it.

Related

Reasons for error in creating pdf file in Laravel

I use the following package to create PDF
https://github.com/niklasravnsborg/laravel-pdf
But in producation and in php 7.4 the case of the following error :
read of 8192 bytes failed with errno=21 Is a directory
an anyone help me?
I was expecting a PDF output
my code is :
$path_certificate = storage_path($create_certificate->template->img);
$path = $create_certificate->template->type == CertificateTypesEnum::PRINTABLE ? 'public/certificates/' : 'public/digital_certificates/';
$type_certificate = $create_certificate->template->type == CertificateTypesEnum::PRINTABLE ? 'pc' : 'dc';
$filename_pdf = auth()->id() . '_edu' . '_' . $create_certificate->education->id . '_' . $type_certificate;
$data = [
'path_cert' => $path_certificate,
'body_inner' => $body_inner,
'user_name' => auth()->user()->name,
'course_name' => $create_certificate->education->title,
'certificate_template' => $create_certificate->certificate_template,
'course_time' => $create_certificate->education->sessions_time / 60,
'score' => number_format($create_certificate->score, 0),
'tracking_code' => $create_certificate->tracking_code,
'date_created' => env('app_locale') == 'fa' ? \Morilog\Jalali\CalendarUtils::strftime('Y-m-d', strtotime($create_certificate->created_at)) : \Illuminate\Support\Carbon::create($create_certificate->created_at)->format('Y/m/d')
];
$pdf_digital = Pdf::loadView('certificate.digital.en_certificate',
$data)->setPaper('a4', 'landscape');

Cannot upload file: (file name), Ckeditor 5 Laravel

Help :(, I'm a newbie trying out CKeditor 5 for my post form
I'm using ckeditor 5, and i'm trying to upload images in it. But, when i'm trying to load image i have a massage: Cannot upload file filename.
Where's the problem?
Init function :
ClassicEditor
.create( document.querySelector( '#body' ), {
ckfinder:{
uploadUrl: "{{ route('ckeditor.upload') .'?token=' . csrf_token()}}"
}
} )
.catch( error => {
console.error( error );
} );
PHP config :
$config['backends']['default'] = array(
'name' => 'default',
'adapter' => 'local',
'baseUrl' => config('app.url').'/userfiles/',
'root' => public_path('/userfiles/'),
'chmodFiles' => 0777,
'chmodFolders' => 0755,
'filesystemEncoding' => 'UTF-8'
);
Controller :
public function uploadImage(Request $request){
if($request -> hasFile('upload')){
$originame = $request->file('upload')->getClientOriginalName();
$fileName = pathinfo($originame, PATHINFO_FILENAME);
$extension = $request->file('upload')->getClientOriginalExtension();
$fileName = $fileName . '_' . time() . '.' . $extension;
$request->file('upload')->move(public_path('media'), $fileName);
$url = asset('media/' . $fileName);
return response()->json(['fileName' => $fileName, 'uploaded'=> 1, 'url' => $url]);
}
}
Thank you, help me :')

Upload base64 image to amazon s3

I'm having some issues when trying to upload an image to AWS S3. It seems to upload the file correctly but, whenever I try to download or preview, it can't be opened. Currently, this is the upload code I'm using:
<?php
require_once 'classes/amazon.php';
require_once 'includes/aws/aws-autoloader.php';
use Aws\S3\S3Client;
$putdata = file_get_contents("php://input");
$request = json_decode($putdata);
$image_parts = explode(";base64,", $request->image);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = $image_parts[1];
$dateTime = new DateTime();
$fileName = $dateTime->getTimestamp() . "." . $image_type;
$s3Client = S3Client::factory(array(
'region' => 'eu-west-1',
'version' => '2006-03-01',
'credentials' => array(
'key' => Amazon::getAccessKey(),
'secret' => Amazon::getSecretKey(),
)
));
try {
$result = $s3Client->putObject(array(
'Bucket' => Amazon::getBucket(),
'Key' => 'banners/' . $fileName,
'Body' => $image_base64,
'ContentType' => 'image/' . $image_type,
'ACL' => 'public-read'
));
echo $result['ObjectURL'] . "\n";
} catch(S3Exception $e) {
echo $e->getMessage() . "\n";
}
?>
So, when I check the console after uploading the image file, it has the expected size, permissions and headers but, as I said, whenever I try to open the file, it fails.
What could be the problem here? Thanks in advance.
The issue here is you appear to be uploading the base64 encoded version of the image and not the raw bytes of the image. Take $image_base64 and decode into raw bytes first http://php.net/manual/en/function.base64-decode.php . I am sure if you tried to open those "images" in a text editor you would see base64 hex data.
you can upload "on the fly" by using the function $s3Client->upload like the following example:
<?php
$bucket = 'bucket-name';
$filename = 'image-path.extension';
$imageData = base64_decode(end(explode(",", $base64)));
$upload = $s3Client->upload($bucket, $filename, $imageData, 'public-read');
$upload->get('ObjectURL');
I use silex php 2.0 and the following code fount
$s3 = $app['aws']->createS3();
$putdata = file_get_contents("php://input");
$data = json_decode($request->getContent(), true);
$data = (object) $data;
$image_parts = explode(";base64,", $data->image);
$image_type_aux = explode("image/", $image_parts[0]);
$image_type = $image_type_aux[1];
$image_base64 = base64_decode($image_parts[1]);
$result = $s3->putObject([
'ACL' => 'public-read',
'Body' => $image_base64,
'Bucket' => 'name-bucket',
'Key' => 'test_img.jpeg',
'ContentType' => 'image/' . $image_type,
]);
var_dump($result['ObjectURL']);

FilesystemAdapter could not be converted to string Laravel

I have a photo upload form which goes to this code
$this->validate($request, [
'image' => 'required|image|max:3000|mimes:jpeg,jpg,png',
]);
$user = Auth::user();
$usersname = $user->username;
$file = $request->file('image');
$ext = $file->getClientOriginalExtension();
$path = Storage::disk('uploads');
$filename = $usersname . '.' . $ext;
if (Storage::disk('uploads')->has($filename)) {
Storage::delete($filename);
}
Storage::disk('uploads')->put($filename, File::get($file));
$resizedImg = Image::make($path . DIRECTORY_SEPARATOR . $filename)->resize(200,200)->save($path . DIRECTORY_SEPARATOR . $filename);
return redirect()->route('profile.index',
['username' => Auth::user()->username]);
}
When I make this code execute it gives me this error
ErrorException in ProfileController.php line 71:
Object of class Illuminate\Filesystem\FilesystemAdapter could not be converted to string
line 71 is the line beginning with $resizedImg but the photo does save to the correct directory just not resized.
I defined uploads in the filesystems.php file as following
'disks' => [
'uploads' => [
'driver' => 'local',
'root' => public_path('/uploads'),
],
$path contents driver in it, but you're trying to use it as string, that's the problem. Try to use something like:
$path = '/uploads';

get extension of uploaded file in cake php

I am using cakephp 2.1 and i am trying to upload files and how can i retrive the extension of the file.
Database/users
Id Auto_Increment
username
file_name
Controller/UsersController.php
public function register(){
if ($this->request->is('post')){
$filename = $this->data['User']['file_name']['name'];
//$temp_ext = $this->data['User']['resume_file']['ext'];
$this->Session->setFlash('Extension : ' . $temp_ext);
}
}
When tried the above code, to get extension. it only gives single letters like L, r ie firt character of the filename but not extension
Now how can i get the extension of the file.. i gone through this link
http://api.cakephp.org/class/file
but could not understand to retrieve the file.
Adding a Debug report to #Julian Hollmann
array(
'User' => array(
'file_name' => array(
'name' => '550992_234300256686731_213914803_n.jpg',
'type' => 'image/jpeg',
'tmp_name' => 'D:\xampp\tmp\php866F.tmp',
'error' => (int) 0,
'size' => (int) 42292
)
)
)
First of all, your data should be in $this->request->data
If you want to see what's in there, just do debug($this->request->data);
Edit:
The correct answer is:
$filename = $this->request->data['User']['file_name']['name'];
$extension = pathinfo($filename, PATHINFO_EXTENSION);
See also php manual
$filename = $this->data['User']['file_name']['name'];
$this->request->data['User']['file_name'] = $filename;
$fileExt = explode(".", $filename);
$fileExt2 = end($fileExt);
Try this one... it will give you extension

Categories