简体   繁体   English

laravel 使用自定义权限上传目录中的图片

[英]laravel upload image in directory with custom permission

I am working on a website that uploads images in sub dir, for example, every time the user uploads an image gets stored in storage/dropzone/upload/timestamp_date/image_name.jpg the problem I faced is the image is not showing up on the client side when I looked on my shared hosting I found that stores function to create a directory with no executable to world permission 700 and I wanted to be 755 or 777.我正在一个在子目录中上传图像的网站上工作,例如,每次用户上传图像时,图像都会存储在 storage/dropzone/upload/timestamp_date/image_name.jpg 中,我遇到的问题是图像没有显示在客户端,当我查看我的共享主机时,我发现存储 function 以创建一个没有可执行文件到世界权限 700 的目录,我想成为 755 或 777。

I tried to change the permissions of my shared hosting files but it works only once cuz when a new directory is created will create it with 700 permission我试图更改我的共享主机文件的权限,但它只工作一次,因为创建新目录时将使用 700 权限创建它

You can create a helper function for common use inside whole application.您可以创建一个 helper function 以在整个应用程序中通用。

use Illuminate\Support\Facades\File;

function checkFolderExists($folder, $permission)
{
    if (!File::isDirectory(base_path('storage/' . $folder))) {
        File::makeDirectory(base_path('storage/' . $folder), $permission , true, true);
    }
    return 'public/' . $folder;
}

where在哪里

$folder = 'dropzone/upload/timestamp_date' $folder = 'dropzone/upload/timestamp_date'

$permission = 0777 or 0775 or your_choice $permission = 0777 或 0775 或 your_choice

And after check the store folder and permission your file store path with store image in that specific folder:在检查存储文件夹并允许您的文件存储路径以及该特定文件夹中的存储图像之后:

$filePath = checkFolderExists('dropzone/upload/timestamp_date', 0775 or 0777 or your_choice);
$fileStoredPath = $request->file('form_name_field')->store($filePath);

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

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