ctaljaardt's avatar

Image S3 Not working

Hello,

Im trying to have a function where a user can change their profile picture, im using S3 and when following the tutorial im getting problems.

Here is a copy of my controller.

<?php namespace App\Http\Controllers;

use App\User; 
use Session;
use Input;
use Illuminate\Http\Request;
use Illuminate\Http\RedirectResponse;
use Illuminate\Contracts\Filesystem\Filesystem;

class UserController extends Controller
{
    public function postChangeImg(Request $request, Filesystem $filesystem)
    {
        $user = User::where('email', $request->user());
        $file = Input::file('file');
        $filesystem->put('15.png', $file);
        dd('done');
    }
}

I named it 15.png for a test, and in the s3 bucket the image is corrupted and only 14bytes in size regardless of the image that i upload.

Does anyone know what im doing wrong?

0 likes
5 replies
bobbybouwmann's avatar

Are you sure you receive an image? Did you update your form with this?

{!! Form::open(['route' => 'upload-image', 'files' => true]) !!}

If you do something like I showed below you should receive an image object. If you receive NULL you need to add the 'files' => true to your form.

dd(Input::file('file'));
ctaljaardt's avatar

@blackbird the image is being recieved by the system as i can do this

dd($file);

Here is the result :

UploadedFile {#27 ▼
  -test: false
  -originalName: "Image 2015-04-14 at 8.03.48 pm.png"
  -mimeType: "image/png"
  -size: 79348
  -error: 0
}   

I was looking into it a little deeper and renamed the image from i downloaded from s3 to .txt at the end and opened it with notepad and here are the contents of it

/tmp/phpVmu5bW
bobbybouwmann's avatar

This means you are not uploading the correct information of the image ;) I think you are uploading the temp name of the file instead of the file itself

ctaljaardt's avatar

yeah becuase if i move the file to a folder and rename it, i go to upload it and the information changes, how would i upload the contents of the image then?

ctaljaardt's avatar
ctaljaardt
OP
Best Answer
Level 5

This is the Solution:

    public function postChangeImg(Request $request, Filesystem $filesystem)
    {            
        $user = User::where('email', $request->user());
        $file = Input::file('file');
        if ($file->getMimeType() != "image/jpeg" && $file->getMimeType() != "image/png") {
            dd('error not image');
        } else {
            $ext = "." . explode(".", $file->getClientOriginalName())[1];
        }
        $filesystem->put('/profile/avatar/' . $request->user()->id . $ext, file_get_contents($file));
        dd('completed);
    }
1 like

Please or to participate in this conversation.