如何在数据库中存储图像路径-使用Laravel

时间:2019-04-17 06:49:40

标签: php laravel image base64

当前我可以将文件名存储为数据库并且存储在laravel的公共文件夹中,但是我想将文件路径存储在数据库中吗?

我的控制器:

$image=$userProfile['image'];
$image1 = base64_decode($image);
$filename = time().'.'.$extension;
Storage::disk('public')->put($filename,$image1);
$this->user->where('id', Auth::user()->id)->update(['profile_pic' => $filename]);

我如何在laravel中存储图像路径并在公共文件夹中存储图像名称?我正在使用base64在json中发送图片。

您的帮助会受到高度赞赏吗?

2 个答案:

答案 0 :(得分:0)

使用:

    /**
     * Try to get real ip from request:
     * <ul>
     *     <li>try X-Forwarded-For</li>
     *     <li>try remote address</li>
     * </ul>
     *
     * @param request    request
     * @return real ip or ""
     */
    private String tryGetRealIp(HttpServletRequest request) {
        // X-Forwarded-For: <client>, <proxy1>, <proxy2>
        // If a request goes through multiple proxies, the IP addresses of each successive proxy is listed.
        // This means, the right-most IP address is the IP address of the most recent proxy and
        // the left-most IP address is the IP address of the originating client.
        String forwards = request.getHeader("X-Forwarded-For");
        if (StringUtils.isNotBlank(forwards)) {
            // The left-most IP must be client ip
            String ip = StringUtils.substringBefore(forwards, ",");
            return ip;
        } else if (StringUtils.isNotBlank(request.getRemoteAddr())) {
            // this could be real client ip or last proxy ip which forwards the request
            return request.getRemoteAddr();
        }
        return "";
    }

答案 1 :(得分:0)

首先键入此命令:

php artisan storage:link

它将创建到公用文件夹的符号链接。

在您的控制器中,您可以这样编写:

if ($request->file('image')) {
    $file = $request->file('image')->store('images', 'public');
    $new_product->image = $file;
 }

此代码将在路径中插入随机字符串,因此您可以在图像列值'images / shgsqwerfsd.jpg'中输入类似的内容

相关问题