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

adhik13th's avatar

Laravel saved file to wrong directory

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 ->

  • app

  • bootstrap

  • config

  • database

  • public_html

    • files ( this file saved here)
  • resources

  • routes

  • storage

  • tests

  • vendor

logs

mail

public_ftp

public_html ->

  • css

  • files (not on here, i need to save here)

  • home

  • images

  • js

  • kalibrasi

  • public

  • sop

  • theme

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 ?

0 likes
17 replies
LynX39's avatar
$file->move(public_path().'/files/', $name);

This code save a file in a Laravel pulic folder. Pass absolute path in move function and change a permission to dir public_html external to Laravel folder.

1 like
adhik13th's avatar

@LYNX39 - i change permission o public_html to 777 and still not upload on public_html

jlrdw's avatar

See my answer here first:

https://laracasts.com/discuss/channels/laravel/next-issue-fonts-and-images-arent-visible

I usually make a define statement such as:

defined('DS') || define('DS', DIRECTORY_SEPARATOR);
define('ROOTDIR', realpath(__DIR__ .'/../../laravel58up') .DS);
define('ASSET', realpath(dirname(__FILE__)). DS . 'assets' . DS);
// or where ever I want assets, or images.
// Do whatever you want for your destination path
// Via helper in laravel, whatever
// But has to have proper permissions

And uploading:

<form action='add' method='post' enctype="multipart/form-data">
        <table style="border:none; width: 700px;">
            <tr>
                <td>dogpic:</td>
                <td>
                    <input name="ufile" type="file" id="ufile" size="50" /></td>
                </td>
            </tr>
             ///////////// more code for form

controller

           $lid = DB::table('dc_dogs')->count();
            $lid = $lid + 1;
            $file = Request::file('ufile');
            $file_name = $file->getClientOriginalName();
            $file_ext = $file->getClientOriginalExtension();

            $fileInfo = pathinfo($file_name);
            $filename = $fileInfo['filename'];
            $newname = $filename . $lid . "." . $file_ext;
            $destinationPath = ASSET . 'upload/imgdogs'; // use your path here not mine.
            $file->move($destinationPath, $newname);

            $dogpic = $newname;
           
            $dogname = ucfirst(Request::input('dogname'));
            $sex = ucfirst(Request::input('sex'));
            $comments = Request::input('comments');
            $adopted = !empty(Request::input('adopted')) ? '1' : '0';
            $lastedit = date("Y-m-d H:i:s");

            $postdata = array(
                'dogpic' => $dogpic,
                'dogname' => $dogname,
                'sex' => $sex,
                'comments' => $comments,
                'adopted' => $adopted,
                'lastedit' => $lastedit
            );

            DB::table('dc_dogs')->insert($postdata);
           

Never had problems with this.

Validation not shown, so validate your stuff as needed.

adhik13th's avatar

@JLRDW - i have problem . i follow this tutorial but , my view on www.domain.com is list of "Index Of" (if u know what i mean) , and for public i create folder named laravel . i access www.domain.com/laravel , its error 500 .

jlrdw's avatar

If storing to a folder above public_html you setup a symbolic link:

https://laravel.com/docs/5.8/filesystem

Or many times on shared hosting you can upload to a folder underneath public_html.

Also wherever you upload to make sure the folder permissions are correct.

adhik13th's avatar

@JLRDW - no no , i remake all my upload file laravel on shared hosting using your tutorial . i following step and finish , i run my website example www.domain.com , if i run this its will showing like homepage, but in my url www.domain.com its showing Index Of .

https://ibb.co/FY4qZJF

its the picture . its wrong on htacces or what ?

jlrdw's avatar

You do have a RewriteBase in your htaccess.

You did point to main laravel with

../../

You should not be able to see the folder structure.

adhik13th's avatar

@JLRDW - i not add RewriteBase on htaccess , its the problem ? and i think '../../' not the problem

adhik13th's avatar

@JLRDW - my folder stricture

  • laravel
  • public_ftp
  • public_html
    • laravel
      • css
      • js

require DIR.'/../../laravel/vendor/autoload.php';

$app = require_once DIR.'/../../laravel/bootstrap/app.php';

i try to add this :

$app->bind('path.public', function() { return DIR; });

jlrdw's avatar

See what happens if you rename the folder that's above public_html to laravelup

And changing the lines to

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/

require __DIR__.'/../../laravelup/vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/

$app = require_once __DIR__.'/../../laravelup/bootstrap/app.php';

And your index.php goes

|- laravelup
     //  main laravel here
|- public_ftp
|- public_html
    |- laravel
         |- index.php   // here
         |- .htaccess   // here
         |-  css
         |-  js
         |- // perhaps put image folder here

jlrdw's avatar

If you go to

yourtsite.com/laravel

replace yoursite.com with actual name you should see:

What do you see, post here, if image just put in an image tag like:

<img src="https://i.imgur.com/RAuhBis.jpg" />

Right click, view image and get url, not site storing image.

adialam's avatar

hey what about now?i have same problem,can you help me,please

Please or to participate in this conversation.