简体   繁体   English

托管 Laravel 不存储图像

[英]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").我有一个托管在 CPanel 中的 Laravel 项目并且它正在运行,但是当我上传图像时,它应该存储在公共文件夹中,如(“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这是存储图像的 function

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谢谢您的帮助

Your function is missing the key requirement to handle the request.您的 function 缺少处理请求的关键要求。 You're not receiving any request in your function upload image.您在 function 上传图片中没有收到任何请求。 You need to receive a request in your function so that you can process further on that request.您需要在 function 中收到请求,以便您可以进一步处理该请求。 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); $request->image->storeAs('images', $imageName); // storage/app/images/file.png // 存储/app/images/file.png

Store Image in Public Folder将图像存储在公共文件夹中

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

Store Image in S3在 S3 中存储图像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM