Adhik Mulat
Adhik Mulat

Reputation: 538

Upload image destination directory file error laravel

I have a function to upload file . on Localhost its not having error . but after i deploy on shared hosting , its have problem . if localhost ,im not moving some folder , but on "tutorial" shared hosting , i need making 2 folder . laravel and public . this laravel folder is all file on project laravel without public

its my schema on my shared hosting

(/home/sippausr)

etc

laravel ->

logs

mail

public_ftp

public_html ->

and i have a function to upload file and saved this file to directory files on public_html like this

public function store6(Request $request)
{
    $this->validate($request, [


    ]);


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



        {   $file = $request->file('image');
            $name=$file->getClientOriginalName();
            $file->move(public_path().'/files/', $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','image']));
    $user->catatan = $request->catatan;
    $user->image=$data;
    $user->status = $request->status;


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

}

but , this file not saved at public_html/files ,

this file saved at Laravel folder , on public_html( you can see at schema) . can someone help me ?

Upvotes: 0

Views: 518

Answers (2)

Natvarsinh Parmar - bapu
Natvarsinh Parmar - bapu

Reputation: 1138

Create a 'files' directory in the public directory

Give permission to files directory

if($request->hasfile('image')){   
        $path = '/files/';
        $file = $request->file('image');
        $name=$file->getClientOriginalName();
        $file->move($path, $name);  
        $data = $name;  
    }

Upvotes: 0

Daniel Luna
Daniel Luna

Reputation: 341

If I understood you want to save files out of Laravel directory and for storing files you are using the "public_path()" method that writes at laravel level directory so a possible solution is to declare a variable in your .env file maybe

PUBLIC_DIRECTORY="/var/www/public_html/" ##Path to your public_directory

and modify your code like this:

if($request->hasfile('image')){   
        $file = $request->file('image');
        $name=$file->getClientOriginalName();
        $file->move(env('PUBLIC_DIRECTORY').'/files/', $name);  
        $data = $name;  
    }

I hope I've helped you

Upvotes: 0

Related Questions