Laravel 存储无法访问
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39752361/
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
Laravel storage can't be accessed
提问by userqwert
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">
该文件夹绝对是符号链接的,因为我可以在 finder 中跟踪它。
回答by userqwert
Fixed this.
修复了这个。
php artisan storage:link
does not work with homestead. 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
.
php artisan storage:link
不适用于宅基地。而是删除文件夹,使用homestead ssh
和在机器上手动重新创建它们ln -s ~/projects/my-website/storage/app/public/ ~/projects/my-website/public/storage
。
回答by Texas Tom
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 facadeto acquire the image src URL?
如果是这样,您是否尝试过使用Storage 门面中的URL 方法来获取图像 src URL?
Example:
例子:
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Storage;
$url = Storage::url('file1.jpg'); //acquire URL
$url = Storage::url('file1.jpg'); //acquire URL
回答by NuOne
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上传图片函数
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
在控制器类中
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()]);
}