Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adhik13th's avatar

How to save upload image to directory in laravel 5.8

i have a function to upload images , this upload type is json . this json like this :

"pertanyaan" => "{"question1":"Kondisi, kebersihan, pelumasan bearing","answer1":"baik","image":"gambar.png"} ◀"

on this json have a key value = image : gambar.png . this name of image is saved into json . and the problem is i cant saved this image (gambar.png) into directory file on folder . my input:

<td> <input class="form-control" style="border:none"  type="file" name="image" value="" readonly> </td>

its my controller :

public function store(Request $request)
{
    if($request->hasfile('image'))
     {

        foreach($request->file('image') as $file)
        {
            $name=$file->getClientOriginalName();
            $file->move(public_path().'/images/', $name);  
            $data = $name;  
        }
     }

    $user = new pemeliharaan;
    $id = Auth::user()->id;

    $user->user_id = $id;
    $user->alat_id = $request->alat_id;
    $user->pertanyaan =json_encode($request->except
    (['_token','name','alat_id','status','catatan']));//this image on here
    $user->catatan = $request->catatan;
    $user->status = $request->status;


    $user->save();
    //dd($user);
    return redirect('user/show6')->with('success', 'Data Telah Terinput');

}

I am create a folder images at public. can someone correct my code to saved file(image) ?

0 likes
33 replies
adhik13th's avatar

i just wanna debuging where my fault code to save in directory .

MaverickChan's avatar

@ADHIK13TH - a very long time ago example, hope it still work

public function upload (Request $request)

    {

        $this->validate($request ,[

            'photo' => 'required|mimes:jpg,jpeg,png,bmp,tif'

            ]);

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

        $filename = time().'_'.$file->getClientOriginalName();

        $path = 'User/'.Auth::user()->name.'/img';

        $thumb = $path.'/thumbnail';

        $file->move($path,$filename);

        $test = $path.'/'.$filename;

        $next = $thumb.'/tn_'.$filename;


        if(!file_exists($thumb)) {

            mkdir($thumb , 0777, true);

        }

        Image::make(public_path($test))
                ->fit(400,400)
                ->save(public_path($next));

        $photo = Photo::create([

                'name' => $filename,

                'path' => $test,

                'thumbnail' => $next,

                'user_id' => Auth::id(),

                'gallery_id' => $request->gallery,

                'size' => File::size($test)


            ]);

    }
adhik13th's avatar

whats wrong about this

'if($request->hasfile('image'))
     {

        foreach($request->file('image') as $file)
        {
            $name=$file->getClientOriginalName();
            $file->move(public_path().'/files/', $name);  
            $data[] = $name;  
        }
     }'
jlrdw's avatar

As @MaverickChan said the csrf token, did you also use enctype="multipart/form-data".

Also did you even look at the links given one is a tutorial:

https://laraveldaily.com/upload-multiple-files-laravel-5-4/

Maybe you should do the tutorial. You may need to forget your code and follow the docs:

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

https://laravel.com/docs/5.8/requests#storing-uploaded-files

and good tutorials that show the correct way.

siangboon's avatar

did you include

 enctype="multipart/form-data" 

in your form?

adhik13th's avatar

@SIANGBOON - form class="form-group" action="/user6" method="post" enctype="multipart/form-data

adhik13th's avatar

@SIANGBOON - 1. {{-- --}}

          <td>
              <label><input type="radio"  name="answer1" value="tidak baik" checked></label>
          </td>
          <td>
              <label><input type="radio"  name="answer1" value="baik"></label>
          </td>
        </tr>
        </div>
      
            <tr>
                <td></td>
                <td> <input class="form-control" style="border:none"  type="file" name="image" value="" readonly> </td>
               
            </tr>
       
        </div>
      
       
        <tr>
            <td></td>
            <td> <div class="form-group">
                <label>Catatan</label>
                <textarea class="form-control" name="catatan" rows="3" placeholder="Enter ..." required></textarea>
             </div>
         
        </tr>

        
    <input type="hidden" name="alat_id" value="7">
    <input type="hidden" name="status"  value="3 Bulanan">             
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    
  </tbody>
 
</table>
      <div class="box-footer">
        <button type="submit" class="btn btn-primary" name="name" value="post">Submit</button>
       </div>
</form>
adhik13th's avatar

and having error

htmlspecialchars() expects parameter 1 to be string, array given

siangboon's avatar

sorry, i don't get you. by the way, did you close your tag properly, the syntax highlight of form code that you shown partially above seem abnormal.

adhik13th's avatar

@SIANGBOON - i adding "enctype="multipart/form-data" but having error "htmlspecialchars() expects parameter 1 to be string, array given "

MaverickChan's avatar

@ADHIK13TH - switch

<input type="hidden" name="_token" value="{{ csrf_token() }}">

to

@csrf

the name of token could be wrong

adhik13th's avatar

@MAVERICKCHAN - i think its not the problem . the problem is using enctype="multipart/form-data" if using this , my json file will like this "image":"{}" , but if not using encypte will like this "image":"img.jpg"

Snapey's avatar

You said in your original question

"image":"gambar.png"

so you are only sending the filename and not the file contents.

This requires the multipart/form-data attribute on the form

To really help, we need to see the <form> tag (in its entirety) and also the code that you are using to send the form via json and not as a regular html upload.

adhik13th's avatar

@SNAPEY - thats the problem . my original content not saving at folder "images" . ok i will show my form . wait a minute

adhik13th's avatar

@SNAPEY - # Pertanyaan Tidak Baik Baik

    <div class="form-group">
        <tr>
          <td>1.</td>
          <td><input class="form-control" style="border:none"   type="text" name="question1" value="Kondisi, kebersihan, pelumasan bearing" readonly></td>
          {{--  <td><input class="form-control" type="text" placeholder=".input-lg"></td>  --}}
          
          <td>
              <label><input type="radio"  name="answer1" value="tidak baik" checked></label>
          </td>
          <td>
              <label><input type="radio"  name="answer1" value="baik"></label>
          </td>
        </tr>
       
            <tr>
                <td></td>
                <td> <input class="form-control" style="border:none"  type="file" name="image" value="" > </td>
               
            </tr>
       
        </div>
      
       
        <tr>
            <td></td>
            <td> <div class="form-group">
                <label>Catatan</label>
                <textarea class="form-control" name="catatan" rows="3" placeholder="Enter ..." required></textarea>
             </div>
         
        </tr>

        
    <input type="hidden" name="alat_id" value="7">
    <input type="hidden" name="status"  value="3 Bulanan">             
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    
  </tbody>
 
</table>
      <div class="box-footer">
        <button type="submit" class="btn btn-primary" name="name" value="post">Submit</button>
       </div>
</form>
adhik13th's avatar

i can add "<form method , encypte,action> at this comment

Snapey's avatar

please format your code using ``` on a line before and after the code

only show the form tag not the whole form

provide the javascript as requested

siangboon's avatar

why not just extract the request() data inside the controller and probably have a method to format accordingly to whatever you like instead of json_encode the request() directly.

Snapey's avatar

Deal with one problem at a time. At present you are storing the image but not saving the path to the file, which you saved in $data. You are not using $data anywhere.

Then you have a problem rendering the new model in the view. This is a totally different issue and is caused by having non-string data passed to the htmlspecialchars function which is what {{ }} does.

adhik13th's avatar

@SNAPEY - how i can saved this $data to my colomn "pertanyaan" on my controller like this

'$user = new pemeliharaan;
$id = Auth::user()->id;

$user->user_id = $id;
$user->alat_id = $request->alat_id;
$user->pertanyaan =json_encode($request->except
(['_token','name','alat_id','status','catatan']));//this image on here , how i can save on here ?
$user->catatan = $request->catatan;
$user->status = $request->status;


$user->save(); '
Snapey's avatar

why do you want to save as json?

Next

Please or to participate in this conversation.