php Laravel 存储链接不适用于生产
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50730143/
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 link won't work on production
提问by mafortis
I'm using storage_path
to save my images and I symbolic my storage folder to public_html
我storage_path
用来保存我的图像并将我的存储文件夹符号化为public_html
php artisan storage:link
On the local everything works fine, when I upload an image it will upload in storage folder and link of it will appear in public
folder but since I moved to live host and production mode my images will upload in storage folder but nothing in my public_html/storage
and I'm not able to get them in my front-end.
在本地一切正常,当我上传图像时,它会上public
传到存储文件夹中,它的链接将出现在文件夹中,但自从我转移到实时主机和生产模式后,我的图像将上传到存储文件夹中,但我public_html/storage
和我什么都没有“我无法在我的前端获取它们。
Codes
代码
/config/filesystems.php
/config/filesystems.php
'public' => [
'driver' => 'local',
'root' => storage_path('app/public/'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
.env
.env
FILESYSTEM_DRIVER=public
controller sample
controller sample
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'page' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = storage_path('app/public/images/' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
if(!empty($page->image)){
Storage::delete('images/' . $page->image);
}
$page->image = $filename;
}
any idea?
任何的想法?
回答by Mostafa Mohsen
I just encountered this problem, and this is how I fixed it, without the need of SSH'ing into the server or running a CronJob:
我刚刚遇到了这个问题,这就是我修复它的方法,不需要通过 SSH 连接到服务器或运行 CronJob:
For Laravel 5+(Haven't tested with lower version)
对于Laravel 5+(未测试过低版本)
You may use Artisan::call()
method inside your route/controller to execute artisan commands without using the terminal.
您可以Artisan::call()
在路由/控制器中使用method 来执行 artisan 命令,而无需使用终端。
$exitCode = Artisan::call('storage:link', [] );
echo $exitCode; // 0 exit code for no errors.
回答by Chetam Okafor
I see that the question has been answered but I want to suggest another solution which i found to be easy.
Go to the route folder your-app-folder/routes/web.php
and register a new route as follows
我看到问题已得到解答,但我想提出另一个我认为很容易的解决方案。进入路由文件夹your-app-folder/routes/web.php
,注册一条新路由如下
Route::get('/linkstorage', function () {
Artisan::call('storage:link')
});
then go to your website www.example.com/linkstorage
and it will take you to a blank page.
that's it. The storage folder will be create. I hope it helps someone.
然后转到您的网站www.example.com/linkstorage
,它将带您到一个空白页面。就是这样。将创建存储文件夹。我希望它可以帮助某人。
回答by Alejandro Roa
change storage_path settings by public_path
通过 public_path 更改 storage_path 设置
'public' => [
'driver' => 'local',
'root' => public_path('app/public_html/'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
],
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = 'page' . '-' . time() . '.' . $image->getClientOriginalExtension();
$location = public_path('app/public_html/images/' . $filename);
Image::make($image)->resize(1200, 600)->save($location);
if(!empty($page->image)){
Storage::delete('images/' . $page->image);
}
$page->image = $filename;
}
if that does not work, you can try:
如果这不起作用,您可以尝试:
$path = base_path().'/../public_html/images';
回答by mafortis
SOLVED
解决了
Thanks for all your helps, I tried to run php artisan storage:link
on my server and I found out that my server has disabled symlink
for security reason.
感谢您的所有帮助,我尝试php artisan storage:link
在我的服务器上运行,但我发现我的服务器symlink
出于安全原因已禁用。
So I have changed all my image uploading functions to upload images in my public
folder instead of storage
folder and now everything is working just fine.
所以我已经改变了我所有的图片上传功能,将图片上传到我的public
文件夹而不是storage
文件夹中,现在一切正常。
回答by Abdel Rahman Kamhawy
delete folder storage From public and run this Command in Cron Job [ one time ]
从公共删除文件夹存储并在 Cron 作业中运行此命令 [ 一次 ]
ln -s /home/public_html/storage/app/public/home/dev5/public_html/public/storage
回答by Nopalm
You also can delete your folder storage:link first then open terminal in host and command php artisan storage:link . If your hosting support symlink it will works
您也可以先删除文件夹 storage:link 然后在主机中打开终端并命令 php artisan storage:link 。如果您的托管支持符号链接,它将起作用
回答by Mr. Piyush Ghosh
Solution: Once your application is uploaded to the live server. Just check that if you are having any storage folder in public directory. Check the folder stucture
解决方案:一旦您的应用程序上传到实时服务器。只需检查您是否在公共目录中有任何存储文件夹。 检查文件夹结构
Just delete the storage folder from the public directory.
只需从公共目录中删除存储文件夹即可。
[app/public/storage]
After that from putty software access your main Laravel project folder, and run this command on live.
之后,从 putty 软件访问您的 Laravel 主项目文件夹,并在 live 上运行此命令。
Remember to configure your .envfile APP_URL={your live server ip} and then the below command.
记得配置你的.env文件 APP_URL={your live server ip} 然后是下面的命令。
php artisan storage:link
It will create a new storage link in public folder associated with the APP_URL.
它将在与APP_URL关联的公共文件夹中创建一个新的存储链接。