上传图片到 Laravel-Lumen API

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48049342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:11:15  来源:igfitidea点击:

Upload Images to Laravel-Lumen API

phplaravellumen

提问by Ali Adil

With my API i need to upload images from mobile app to my server and save the image path to database so i have the following issues :

使用我的 API,我需要将图像从移动应用程序上传到我的服务器并将图像路径保存到数据库,因此我有以下问题:

  • Where to save the images? I tried to save them in the storage/app under images folder (which is work fine)

       public function fileUpload(Request $request) {
    
    
       if ($request->hasFile('image')) {
           $image = $request->file('image');
           $name = time().'.'.$image->getClientOriginalExtension();
           $destinationPath = storage_path('/app/images');
           $image->move($destinationPath, $name);
    
    
           return response()->json(['data'=>"image is uploaded"]);
       }
    

    }

    and make a symbolic link to this folder in the public folder but its not working due to access permission error which will lead to the other issue.

  • What permission should i gave to the storage folder to make the whole operation works save the images and the saved link is readable (even 777 didn't work) Access forbidden!

    sudo chmod -R 777 storage

  • 图片保存在哪里?我试图将它们保存在图像文件夹下的存储/应用程序中(工作正常)

       public function fileUpload(Request $request) {
    
    
       if ($request->hasFile('image')) {
           $image = $request->file('image');
           $name = time().'.'.$image->getClientOriginalExtension();
           $destinationPath = storage_path('/app/images');
           $image->move($destinationPath, $name);
    
    
           return response()->json(['data'=>"image is uploaded"]);
       }
    

    }

    并在公共文件夹中创建指向此文件夹的符号链接,但由于访问权限错误而无法正常工作,这将导致另一个问题。

  • 我应该给存储文件夹什么权限才能使整个操作有效保存图像并且保存的链接可读(即使777也不起作用)禁止访问!

    须藤 chmod -R 777 存储

Any ideas will be much appreciated

任何想法将不胜感激

回答by YouneL

Storage directory is not open to user's requests, public directory is, you need to create a symbolic link from storage directory to public directory:

存储目录不对用户请求开放,公共目录是,您需要创建一个从存储目录到公共目录的符号链接:

php artisan storage:link

This command will create a symbolic link from public/storageto storage/app/public, in this case you can store your file under storage/app/publicand make it accessible from the web too:

此命令将创建一个从public/storageto的符号链接storage/app/public,在这种情况下,您可以将文件存储在下面storage/app/public并使其也可以从网络访问:

$image = $request->file('image');
$image->storeAs('public', $name); // => storage/app/public/file.img
URL::asset('storage/'.$name); // => http://example.com/stoage/file.img

回答by Chisom Maxwell

I would advice you try to create a folder in the public folder and store your files there. You can use base_path()."/public/uploads"

我建议您尝试在公共文件夹中创建一个文件夹并将您的文件存储在那里。您可以使用 base_path()."/public/uploads"