upload image

Published 3 months ago by davy_yg

Hello,

I am trying to create upload image form:

seller.blade.php

<form action="{{ url('/daftarseller') }}">
<div id="upload">
KTP:
<input type="file" class="form-control" name="foto_profile" accept="image/png,image/jpeg" required><br>
Profile:
<input type="file" class="form-control" name="foto_ktp" accept="image/png,image/jpeg" required>

<br>
<input type="submit" value="Submit">
</form>

SellerController.php

public function add(Request $request)
{

    $original_name_ktp = $request->file('foto_ktp')->getClientOriginalName();
    $original_name_profile = $request->file('foto_profile')->getClientOriginalName();


    $request->file('foto_ktp')->storeAs(null, $original_name_ktp, 'foto_ktp');      
    $request->file('foto_profile')->storeAs(null, $original_name_ktp, 'foto_profile');

}

I get an error on:

$original_name_ktp = $request->file('foto_ktp')->getClientOriginalName();

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Call to a member function getClientOriginalName() on null

I am not sure what it does I basically want to copy the file that upload to the : foto_ktp directory (I already set it up on filesystems.php).

I also need to save the file name of the picture. Please help make this works.

Best Answer (As Selected By davy_yg)
jlrdw

In xampp, set up your development correctly following this guide

http://novate.co.uk/deploy-laravel-5-on-shared-hosting-from-heart-internet/

Should look like this https://imgur.com/Oo6k4Fp

Then choose if you want images above public_html(htdocs) or under public_html

It's just good to setup dev env correctly instead of all in one folder under htdocs.

In laravel most store images under storage which is above htdocs. But choice is yours, you can also store in public folder, if non-secure and okay for public to gain access to.

I feel you would benefit from a proper laravel folder structure prior to anything else.

topvillas

<form action="{{ url('/daftarseller') }}" enctype="multipart/form-data">

And you need to check that it isn't null.

davy_yg

I set it up to be nullable in the table since it is the same table as the login page and I leave it blank for the first time and request to fill it in after the user login.

I already upload 2 files since there are two browse buttons. So there should be a file in each file upload button.

topvillas

Do you take any notice of anything anybody says?

it doesn't matter if you made the database field nullable. You still need to either check that a file has been uploaded or validate the post.

But it will always return null unless you add the enctype to the form element. As I told you in the reply above.

davy_yg

This is a bit strange:

SellerController.php

public function add(Request $request)
{

    $this->validate($request, [
        'no_ktp' => 'required',
        'foto_ktp' => 'required|image|mimes:jpg,jpeg,png|max:10000',
        'foto_profile' => 'required|image|mimes:jpg,jpeg,png|max:10000'
        ]);

    $original_name_ktp = $request->file('foto_ktp')->getClientOriginalName();
    $original_name_profile = $request->file('foto_profile')->getClientOriginalName();

    $request->file('foto_ktp')->storeAs(null, $original_name_ktp, 'foto_ktp');      
    $request->file('foto_profile')->storeAs(null, $original_name_profile, 'foto_profile');

    session()->flash('flash', 'Thank you, your registration completed');

    return redirect('/seller');


}

I get this message:

The foto ktp must be an image.
The foto ktp must be a file of type: jpg, jpeg, png.
The foto profile must be an image.
The foto profile must be a file of type: jpg, jpeg, png.

I wonder why it does not pass the validation test?

I already uploaded a jpg & jpeg file and they are images. I uploaded 180 KB jpeg & 30 KB jpg files.

topvillas

That's it, I give up.

davy_yg

seller.blade.php

@include('layouts.errors')
@include('layouts.flash')
@include('layouts.errors_html')

<form action="{{ url('/daftarseller') }}" method="get" enctype="multipart/form-data">


<div id="box">
<hr>
<div id="langkah">LANGKAH 1</div>
<hr><br>

Masukkan No. KTP <input class="effect" name="no_ktp" type="textbox" placeholder="No. KTP" size="20"><br>
<br><br>
</div>  

<div id="box2">
<hr>    
<div id="langkah">LANGKAH 2</div>
<hr><br>

Foto diri beserta KTP Anda.<br>
Nomor KTP dan Wajah Anda harus terlihat jelas<br>
dalam foto.<br><br>

<div id="upload">
<br>

<div class="col-sm-6 col-xs-6">

<img src="images/reg/upload_foto.jpg" width="150">
<input type="file" class="foto_upload" name="foto_profile" accept="image/png,image/jpeg" required></div>

<div class="col-sm-6 col-xs-6">

<img src="images/reg/upload_ktp.jpg" width="150">
<input type="file" class="ktp_upload" name="foto_ktp" accept="image/png,image/jpeg" required></div>

<div class="col-sm-6 col-xs-6">Tambahkan Foto Anda</div>
<div class="col-sm-6 col-xs-6">Tambahkan Foto KTP Anda</div>


</div>

</div>

<br><br><br><br>

<div id="box3"><br>
<input type="checkbox" name="syarat_penjual" value="true"> Saya setuju dengan   <a href="https://shopee.co.id/docs/3000" target="_blank">Syarat & Ketentuan</a> program Penjual Terpilih Shopee.<br>
</div>

<br><br>
<div id="box4"><br>
<img src="images/reg/btn_kirim.jpg" width="300">
<input type="submit" value="Submit">
</div>

</form>

Perhaps if anyone else has an idea?

topvillas

sigh

You can't use get if you're uploading files. How would that possibly work?

davy_yg

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

Is it okay to write the method in laravel:

<form action="{{ url('/daftarseller') }}" method="post" enctype="multipart/form-data">

My other page is without method but it still works.

I don't understand why part of the error shows:

GET Data

no_ktp "12345678"

foto_profile "KTP Davy.jpeg"

foto_ktp "photo.jpg"

syarat_penjual "true"

POST Data empty

I already set it "post".

topvillas

The form method and enctype has nothing to do with laravel.

It's HTTP 101.

I'm out of here.

jlrdw
jlrdw
3 months ago (237,650 XP)

Code deleted

I'm out to

I posted code here than works 100%, but totally ignored.

Cronix
Cronix
3 months ago (647,500 XP)

run php artisan view:clear. The old form could be cached and not picking up your changes.

davy_yg

I finally get it to works after refreshing my browser and refill the information.

One problem:

I have not been successful copying the file to the server.

SellerController.php

public function add(Request $request)
{

    $this->validate($request, [
      'no_ktp' => 'required|numeric',
      'foto_ktp' => 'required|image|mimes:jpg,jpeg,png|max:100',
      'foto_profile' => 'required|image|mimes:jpg,jpeg,png|max:100',
      'syarat_penjual' => 'required'
    ]);

    $original_name_ktp = $request->file('foto_ktp')->getClientOriginalName();
    $original_name_profile = $request->file('foto_profile')->getClientOriginalName();

    // storing file 
    $request->file('foto_ktp')->storeAs(null, $original_name_ktp, 'foto_ktp');      
    $request->file('foto_profile')->storeAs(null, $original_name_profile, 'foto_profile');


    session()->flash('flash', 'Thank you, your registration completed');

    return back();


}

The foto_ktp folder and foto_profile folder remains empty.

filesystems.php

 'foto_ktp' => [
      'driver' => 'local',
      'root' => public_path() . '/images/foto_ktp',
      'url' => env('APP_URL').'/images/foto_ktp',
      'visibility' => 'public',
    ],

    'foto_profile' => [
      'driver' => 'local',
      'root' => public_path() . '/images/foto_profile',
      'url' => env('APP_URL').'/images/foto_profile',
      'visibility' => 'public',
    ],
Cronix
Cronix
3 months ago (647,500 XP)

In your validation rules, remove either the "image" rule, or the "mime_types" rule. They do the same thing, so don't use both. If you only want to allow the mime types you specified (jpg/jpeg/png), then remove the image rule from both. That's why you're getting double error messages like:

The foto ktp must be an image.
The foto ktp must be a file of type: jpg, jpeg, png.

From the docs:

If you would not like a file name to be automatically assigned to your stored file, you may use the storeAs method, which receives the path, the file name, and the (optional) disk as its arguments:

You're setting null for the path.

https://laravel.com/docs/5.6/filesystem#file-uploads

davy_yg

So should I change that code to:

$request->file('foto_ktp')->storeAs('foto_ktp', $original_name_ktp, 'foto_ktp');      
$request->file('foto_profile')->storeAs('foto_profile', $original_name_profile, 'foto_profile');

to make it works?

I need the actual file that I upload actually copied to the server folder.

Cronix
Cronix
3 months ago (647,500 XP)

How would I know what path you want to store them in?

Please sign in or create an account to participate in this conversation.