简体   繁体   English

Laravel 存储无法访问

[英]Laravel storage can't be accessed

I am writing a system to upload and show some images, however I am confused about the correct way to access them once uploaded.我正在编写一个系统来上传和显示一些图像,但是我对上传后访问它们的正确方法感到困惑。

public function storeImage(Request $request)
{

    $request->file('image')->store('public/images');

    $filename = md5_file($request->file('image')) . '.' .  $request->file('image')->extension();
    $img = new Image;
    $img->path = 'storage/images/'.$filename;
    $img->save();

    return back();
}

This works fine, and I have symlinked storage as suggested, however if I use the $filename variable from the database I get a 404. What is the correct path to generate to insert as an inline image src attribute here?这工作正常,并且我按照建议进行了符号链接storage ,但是如果我使用数据库中的$filename变量,我会得到 404。生成插入为内联图像 src 属性的正确路径是什么?

The image tag looks like this: <img src="storage/images/89cdd31f2e0eeacceff96c2a0abb252f.jpeg"> The folder is definitely symlinked as I can follow it in finder.图像标签如下所示: <img src="storage/images/89cdd31f2e0eeacceff96c2a0abb252f.jpeg">该文件夹绝对是符号链接的,因为我可以在查找器中跟踪它。

Fixed this. 解决此问题。

php artisan storage:link does not work with homestead. php artisan storage:link不适用于宅基地。 Instead delete the folder, manually recreate them on the machine using homestead ssh and ln -s ~/projects/my-website/storage/app/public/ ~/projects/my-website/public/storage . 而是删除文件夹,使用homestead sshln -s ~/projects/my-website/storage/app/public/ ~/projects/my-website/public/storage在计算机上手动重新创建文件夹。

Are you using the latest version of laravel, 5.3? 您是否在使用最新版本的laravel 5.3?

If so, have you tried using the URL method from the Storage facade to acquire the image src URL? 如果是这样,您是否尝试过使用Storage Facade中URL方法来获取图像src URL?

Example: 例:

use Illuminate\\Support\\Facades\\Storage;

$url = Storage::url('file1.jpg'); //acquire URL

files should be stored in the directory storage/app/public so make sure that your files are stored in this directory. 文件应存储在目录storage/app/public因此请确保文件存储在此目录中。

After saving the file make sure to get the url of the file by passing file name as the argument.. here how I implemented 保存文件后,请确保通过传递文件名作为参数来获取文件的url。

html form html表格

<form action="" method="post" id="jobImage">
   <div class="form-group">
     <input type="file" class="form-control" id="image" name="image">
   </div>
   <input type="button" value="Upload Image" onclick="uploadImage()" class="btn btn-default" />
</form>

jquery uploadImage function jQuery uploadImage函数

function uploadImage(){
        $.ajax({
            url:"{{url('/')}}/uploadImage",
            headers: {
                'X-CSRF-TOKEN': "{{csrf_token()}}"
            },

            data:new FormData($("#jobImage")[0]),
            method:"POST",
            processData: false,
            contentType: false,
            success:function(data){
                console.log(JSON.stringify(data));
                // alert(JSON.stringify(data));
                // $('#jobImageToDisplay').attr('src', '/public/uploads/' + data.image);

                $('#jobImageToDisplay').attr('src', data.url);
            },
            error:function(error){
                console.log(error);
            }
        });
    }

route in web.php 在web.php中路由

Route::post('/uploadImage','ControllerClass@uploadImage');

In the Controller class 在Controller类中

public function uploadImage(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);


        if ($validator->passes()) {

            $input = $request->all();
            $uploadedImage = $input['image'];
            // put image to storage/app/public/uploads/images
            $name = Storage::put('/public/uploads/images/', $uploadedImage);

            $visibility = Storage::getVisibility($name);
            // set visibility to true
            Storage::setVisibility($name, 'public');

            // get the url of the uploaded file
            $url = Storage::url($name);
            // return the url
            return response()->json(['url' => $url]);
        }


        return response()->json(['error'=>$validator->errors()->all()]);
    }

Also make sure you provide the correct APP_URL in the.env file还要确保在 .env 文件中提供正确的 APP_URL

config/filesystems.php配置/文件系统.php

'public' => [
    'driver' => 'local',
    'root' => storage_path('app/public'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
    'throw' => false,
],

.env .env

APP_URL=http://127.0.0.1:8000

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

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