October: Base64 attachOne

Created on 10 Apr 2017  路  7Comments  路  Source: octobercms/october

$assignment = Model::find($planned);
$data = base64_decode(input('base64'));
$assignment->image()->create(['data' => $data]);
$assignment->save();

When I want to save an image from the request, I get an error saying: exception 'ErrorException' with message 'is_file() expects parameter 1 to be a valid path, string given' in xxxxxxxx/vendor/symfony/http-foundation/File/File.php:36.

Is there another way of attaching an image to a model?

Question

All 7 comments

Besides writing it to a temp file, I can't think of any other way

$data = base64_decode(input('base64'));
$path = temp_path(md5($data));
File::put($path, $data);
$assignment->image = $path;
$assignment->save();

Thanks @daftspunk, I will try this!

@daftspunk hi, i tried your solution but it throws this error:

Call to undefined method October\Rain\Database\QueryBuilder::put() \/home\/hesabdar2\/domains\/amoozandeh.ir\/public_html\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Query\/Builder.php 2123

@salehasadi use the File helper class, not the File model class. I.e. use File as FileHelper; then FileHelper::put() not use System\Models\File;

@LukeTowers thank you i'll check it.
i found another solution in stack overflow with fromData method my october version was old i updated it and method Became available.

@salehasadi ah yes, definitely go with File::fromData instead

use File as FileHelper;

    $data=request('image');
    list($type, $data) = explode(';', $data);
    list(, $data)      = explode(',', $data);
    $data = base64_decode($data);

    $path = temp_path(md5($data)).'.png';
    FileHelper::put($path, $data);
    $assignment= Cv::find(post('cv_id'));

    $assignment->profile = $path;
    $assignment->save();
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jvanremoortere picture jvanremoortere  路  3Comments

lukaszbanas-extremecoding picture lukaszbanas-extremecoding  路  3Comments

atrauzzi picture atrauzzi  路  3Comments

sozonovalexey picture sozonovalexey  路  3Comments

m49n picture m49n  路  3Comments