Search code examples
phpimagelaravelbase64laravel-storage

Image saved always empty with Storage::put in Laravel


I am trying to save a file using Storage::put but it always saves an empty image file and I don't understand why.The file I try to save is base64 encoded.

Here is my code:

  $image = str_replace('data:image/png;base64,', '', $file);
  $image = str_replace(' ', '+', $image);
  $decodedFile = base64_decode($image);


  // Image Size
  $size = strlen($decodedFile);

  // Image Mime Type
  $f = finfo_open();
    $mimeType = finfo_buffer($f, $decodedFile, FILEINFO_MIME_TYPE);
  finfo_close($f);

  // Extension
  $extension = explode('.', $filename)[1];
  $error = null;
  $test = $public;

  // Save file locally
  Storage::put('tmp/temporary', $decodedFile);

  $object = new UploadedFile($url, $filename, $mimeType, $size, $error, $test);

This is what I get as result, size is always 0

UploadedFile {#307 ▼
-test: false
-originalName: "wassho-omni-shoreham-hotel-marquee-lounge.jpg"
-mimeType: "image/png"
-size: 185463
-error: 0
path: "na_source/public/storage/tmp"
filename: "temporary"
basename: "temporary"
pathname: "na_source/public/storage/tmp/temporary"
extension: ""
realPath: "na_source/storage/app/public/tmp/temporary"
aTime: 2017-10-19 21:59:45
mTime: 2017-10-19 21:03:12
cTime: 2017-10-19 21:59:07
inode: 11166493
size: 0
perms: 0100644
owner: 1000
group: 1000
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
}

Someone knows what I am doing wrong here?


Solution

  • In the end, it was not completely clear to me why, but exchanging Storage::put function with file_put_contents($url, $decodedFile); function solved the issue, and it saved the file without a hitch using above code.