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

sonzu's avatar
Level 1

public folder conflict in shared hosting

I host my laravel project in a shared hosting server following the common process of creating the folder out of the public_html directory. Everything is good and working nice. Problem is that when any user upload a file like image, it does not save in the public folder insie public_htm, rather it is stored in the outsidr project folder creating a new public directory. I tried storing the total project in the public_html folder too, but the file storing and retriving problem is not changed. The blade serches file in the public folder that is stored in public_html. Any senor please suggest me what to do?

0 likes
6 replies
Hujjat's avatar

When stored the uploaded file, How you did that? Share your controller. I hope you did something like this.

'''

 $request->customer_photo->move(public_path('customer-photos'), $photoName);       

'''

sonzu's avatar
Level 1

Thanks senor for your response.

Ya, i follwed the similiar process,

this is my controller

if($request->hasFile('photo')){
            // check db & delete old photo first and empty the db value
            if(!empty($personals->photo)){
               if(file_exists(public_path().'/media/applicant_photo/'.$request->delete_photo)){
                    unlink(public_path().'/media/applicant_photo/'.$request->delete_photo);
                }
                $personals->photo = '';
            }
            // update new photo and save it
            $fileName = date('Y_m_d_h_i_s_') . $request->file('photo')->getClientOriginalName();
            $dirPath = public_path() . '/media/applicant_photo/';

            $request->file('photo')->move($dirPath, $fileName);
            $personals->photo = $fileName;
        }

the file should be stored in the

public_html/my_project/media/applicant_photo/flle_name.jpg,

but it goes to outer project folder that is created outside of the public_html

my_project/public/media/applicant_photo/file_name.jpg,

though there is no public folder in that directory, it is created automically when the storing process runs.

I am still searching and applying differenty but all goes failed.

Snapey's avatar

The public_path seems to be hardcoded

/**
 * Get the path to the public / web directory.
 *
 * @return string
 */
public function publicPath()
{
    return $this->basePath.DIRECTORY_SEPARATOR.'public';
}

You could create your own function or override the path bound into the app container.

function public_html_path()
{
   return $this->basePath.DIRECTORY_SEPARATOR.'public_html';
}
sonzu's avatar
Level 1

Hello senor Snapy, I tried your suggested option and it worked too. But for some technical cause I had to keep all the folders in a single directory in public_html. I made some changes required on the basis of that directory setting and it worked fine. I added index files in all the sub-folders inside the public folder to stop directory access. It worked fine too. Everything is exactly set as I wanted. I just wanna ask you if there any problem that I created 2 index files (.html & .php) inside every sub-folder inside the public folder?

Thanks again senor.

Snapey's avatar

you can prevent directory listing with htaccess or web server config. it's not necessary to create a file.

But, have you checked that someone cannot just open the .env file?

1 like
sonzu's avatar
Level 1

Thanks for your response senor Snapey, I checked the possible chance to access .env and it is secured I found. And I thought I should not write anything without learning what may be the after effect in .htaccess. ya, you are right. I think I can secure the directory through . htaccess.

Thank you again senor. I really like your suggestions.

Please or to participate in this conversation.