Search code examples
phplaravelhost

hosted Laravel not storing images


I have a Laravel project hosted in CPanel and it's running but when I upload an image, it supposed to store in the public folder like ("public_path()/posts/theactualimage.jpeg").
the image path is stored in the database but the real image is actually not stored at all

in my local machine things are going pretty well but not on the hosted

I don't know if there's any configuration that I should do ...

here is the structure of the folders

./
|    public_html
|    |    posts
|    |    index.php 
|    |    some other files
|
|    myapp
|    |    all the other files controllers views ...

here is the function that stores the images

public function uploadImage($location, $imageName){
    $name = $imageName->getClientOriginalName();
    $imageName->move(public_path().'/'.$location, date('ymdgis').$name);
    return date('ymdgis').$name;
}

I did not use link storage
thank you for your help


Solution

  • Your function is missing the key requirement to handle the request. You're not receiving any request in your function upload image. You need to receive a request in your function so that you can process further on that request. Here's a sample code for your better understanding.

       /**
         * Storing an Image in public folder.
         *
         * @return \Illuminate\Http\Response
         */
        public function uploadImage(Request $request, $location, $imageName)
        {
            $request->validate([
                'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            ]);
        
            $imageName = time().'.'.$request->image->extension();  
         
            $request->image->move(public_path('images'), $imageName);
      
            /* Store $imageName name in DATABASE from HERE if you want.  */
        
            return back()
                ->with('success','You have successfully upload image.')
                ->with('image',$imageName); 
        }
    }
    

    Store Image in Storage Folder

    $request->image->storeAs('images', $imageName); // storage/app/images/file.png

    Store Image in Public Folder

    $request->image->move(public_path('images'), $imageName); // public/images/file.png

    Store Image in S3

    $request->image->storeAs('images', $imageName, 's3');