NoneNameDeveloper's avatar

Image Upload Doesn't Work | Laravel 5.5

I am using intervention image and now I have doesn't work upload image. I don't now what's happened! No error or anything, But doesn't upload it to table. There my controller Now i have two way upload avatar, But both of them doesn't work. First:

$image_icon = $request->file('image_icon');
if($image_icon){
\File::delete(public_path() .'/upload/users/'.$user->image_icon.'-b.jpg');
\File::delete(public_path() .'/upload/users/'.$user->image_icon.'-s.jpg');
$tmpFilePath = 'upload/users/';
$hardPath = substr(0,100).'_'.time(); $img = Image::make($image_icon); $img->save($tmpFilePath.$hardPath.'-b.jpg'); $img->fit(100, 100)->save($tmpFilePath.$hardPath. '-s.jpg'); $user->image_icon = $hardPath;

    }
Second Controller is:
 $icon = $request->file('user_icon');
        if($icon){
            $tmpFilePath = 'upload/users/';
            $hardPath =  str_slug($inputs['first_name'], '-').'-'.md5(time());
            $img = Image::make($icon);
            $img->fit(250, 250)->save($tmpFilePath.$hardPath.'-b.jpg');
            $user->image_icon = $tmpFilePath.$hardPath.'-b.jpg';
        }
My Migration
$table->string('image_icon')->nullable();
And my profile.blade
< I nput type="file" name="featured_image" id="input-file" >
< I nput type="file" name="user_icon" id="input-file">
I don't understanding where is error, Becosue when i save it all is as well... But when i useing it in Laravel 5.2,5.3, 5.4 versions all was working.
0 likes
13 replies
silverxjohn's avatar

did you add an enctype attribute to your form?

<form action="/some/url" method="post" enctype="multipart/form-data">
NoneNameDeveloper's avatar

@silverxjohn yeah, I have added it like {!! Form::open(array('url' => 'profile','class'=>'','id'=>'myProfile','role'=>'form','enctype' => 'multipart/form-data')) !!} But no solution, No error while uploading, But nothing uploaded to file (upload/users) and Table(users/image_icon)

Mo7sin's avatar

Maybe some debugging would help, in your store method dd(request()->all()) (die and dump) the request, so you can see what is returned.

Snapey's avatar

just work through it step by step. we can review your code but a big assumption is that you actually pass a file and that the form element is called user_icon

Maso's avatar
$hardPath = substr(0,100).'_'.time();

substr of what?

NoneNameDeveloper's avatar

@Mo7sin @Snapey thanks, but I don't now where is problem. There is my full controller (editprofile), and what's wrong I don't understanding, I have all setting configured in my opinion. use Intervention\Image\Facades\Image; (Intervention image)

public function editprofile(Request $request) { $data = \Input::except(array('_token')) ; $inputs = $request->all(); $rule=array( 'username' => 'required|string|max:255', 'first_name' => 'required|string|max:255', 'last_name' => 'required|string|max:255', 'email' => 'required|email|max:200' );

     $validator = \Validator::make($data,$rule);
    if ($validator->fails())
    {
            return redirect()->back()->withErrors($validator->messages());
    } 
      
    $user_id=Auth::user()->id;
    $user = User::findOrFail($user_id);
    $icon = $request->file('user_icon');
    if($icon){
        $tmpFilePath = 'upload/users/';
        $hardPath =  str_slug($inputs['first_name'], '-').'-'.md5(time());
        $img = Image::make($icon);
        $img->fit(250, 250)->save($tmpFilePath.$hardPath.'-b.jpg');
        //$img->fit(80, 80)->save($tmpFilePath.$hardPath. '-s.jpg');
        $user->image_icon = $tmpFilePath.$hardPath.'-b.jpg';
    }
    $user->first_name = $inputs['first_name']; 
    $user->last_name = $inputs['last_name'];       
    $user->email = $inputs['email'];
    $user->username = $inputs['username'];   
    $user->save();

        return \Redirect::back()->with('flash_message', 'Profile updated!');
}    
Mo7sin's avatar

If I where you, I will dd(request()->all());

use it and paste whatever you've got here.

NoneNameDeveloper's avatar

@Mo7sin When I paste your request, I have displayed this window

array:17 [▼
"_token" => "IKbyUr1X3SB0lKT1LYMk7T5twP7qAdAm6Ie9AQcy"
"first_name" => "Jhon"
"last_name" => "Doee"
"username" => "usernameone"
"email" => "[email protected]"
"cat_id" => "1"
"mobile" => "+1234567890"
"contact_email" => "[email protected]"
"website" => "https://demo.info/"
"address" => "USA/NewYork"
"about_me" => "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an ▶"
"facebook_url" => "http://facebook.com/"
"twitter_url" => "http://twitter.com/"
"linkedin_url" => "http://facebook.com/"
"google_url" => "https://"
"youtube_url" => "https://"
"user_icon" => UploadedFile {#227 ▶}
]
Also inside
"
"user_icon" => UploadedFile {#227 ▶}
]
displayed it
-test: false
-originalName: "8fde6fb3d9c2072dadcf5be3d11a36d1--movie-db-baby-movie[1].jpg"
-mimeType: "image/jpeg"
-size: 22794
-error: 0
#hashName: null
path: "C:\OSPanel\userdata\temp"
filename: "phpD585.tmp"
basename: "phpD585.tmp"
pathname: "C:\OSPanel\userdata\temp\phpD585.tmp"
extension: "tmp"
realPath: "C:\OSPanel\userdata\temp\phpD585.tmp"
aTime: 2017-12-18 02:09:38
mTime: 2017-12-18 02:09:38
cTime: 2017-12-18 02:09:38
inode: 0
size: 22794
perms: 0100666
owner: 0
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "C:\OSPanel\userdata\temp\phpD585.tmp"
}
]

impbob36's avatar

Your first controller is looking for image_icon

$image_icon = $request->file('image_icon');

... but your request above shows user_icon

1 like

Please or to participate in this conversation.