skip to Main Content

So, i have a folder in "public" where uploaded files goes to.
On server i have the project outside the public_html folder and only the index there.
Already modified the paths to point to the **public_html **folder.
Unfortunally, when i try to upload a file, instead of that file goes to the public_html folder.
Laravel creates a public folder and uploads the files to instead.

When i echoed the public_path() it pointed to where the project is and the public folder inside it instead of pointing to the public_html folder.

Here’s the code:

function saveImageWithName($name,$photo,$folder)
    {
        $file_extension = $photo -> getClientOriginalExtension();
        $file_name = $name . '.' . $file_extension;
        $path = $folder;
        $this->removeImage($path . $file_name);
        $photo -> move($path,$file_name);
        return $file_name;
    }

    function removeImage($path)
    {
        if (!File::exists($path))
            return true;
        return File::delete($path);
    }

Note: i know about the storage. But i need to upload the files to the public folder it self. The one that is public_html.

Not2: .env file. if its on both local or production the same problem happens.

Note3: I Also tried chmoed the folder to 777 and it also didnt fix it.

Note4: The php code above works on other projects with the same php version and laravel version. This is just the first time im facing this problem.

2

Answers


  1. Chosen as BEST ANSWER

    Well, i managed to do a workaround. Since, all the suggestions didnt work at all for me. Although i still dont know exactly what or why this problem even happened.

    So what i have done is as following:

    in index.php file:

    Define('ROOT_DIR',__DIR__);
    

    and in my class that handles uploading and storing file i just added ROOT_DIR before the path.

    and it works currently very well.


  2. Don’t use public_path () function, you should use url ()

    public_path (‘public_html/a.jpg’) returns the storage path, for example:
    C:webrootprojectpublicpublic_html

    Url (‘public_html/a.jpg’) returns the access address, for example:
    http://example.com/public_html/a.jpg

    But I suggest you use filesystem, which can also use public directories instead of storage.

    // laravelProjectconfigfilesystems.php
    'disks' => [
    
        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
            'throw' => false,
        ],
    
        'public_html' => [
            'driver'     => 'local',
            'root'       => public_path('public_html'),     // Pay attention here
            'url'        => env('APP_URL').'/public_html',  
            'visibility' => 'public',
            'throw'      => false,
        ],
    
        ...
    

    If you rename the public directory to public_html, you need to redefine and override the public function

    // in your function file
    function public_path($path = ''){
        return base_path('public_html/' . ltrim($path, '/'));
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search