【问题标题】:How can i change the image upload directory and view image url in laravel如何更改图片上传目录并在 laravel 中查看图片 url
【发布时间】:2021-09-30 22:49:37
【问题描述】:

在我的脚本中,所有上传的图像都进入目录“ib”,但我想将该目录更改为不同的名称,例如。 "imgib"

这是我到目前为止所做的。我将代码值从“ib”更改为“imgib”

                } else {
                    // upload path
                    $path = 'imgib/';
                    // if path not exists create it
                    if (!File::exists($path)) {
                        File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);
                    }
                    // move image to path
                    $upload = $request->file('uploads')->move($path, $imageName);
                    // file name
                    $filename = url($path) . '/' . $imageName;
                    // method server host
                    $method = 1;
                }
                // if image uploded
                if ($upload) {
                    // if user auth get user id
                    if (Auth::user()) {$userID = Auth::user()->id;} else { $userID = null;}
                    // create new image data
                    $data = Image::create([
                        'user_id' => $userID,
                        'image_id' => $string,
                        'image_path' => $filename,
                        'image_size' => $fileSize,
                        'method' => $method,
                    ]);
                    // if image data created
                    if ($data) {
                        // success array
                        $response = array(
                            'type' => 'success',
                            'msg' => 'success',
                            'data' => array('id' => $string),
                        );
                        // success response
                        return response()->json($response);
                    } else {
                        if (file_exists('imgib/' . $filename)) {$delete = File::delete('imgib/' . $filename);}
                        // error response
                        return response()->json(array(
                            'type' => 'error',
                            'errors' => 'Opps !! Error please refresh page and try again.',
                        ));
                    }

到目前为止一切正常,它会自动创建“imgib”目录,所有上传都进入“imgib”目录。

但问题是,图片 url 仍然使用相同的旧目录名称。

例如。 site.org/ib/78huOP09vv

如何使其获得正确的网址,例如。 site.org/imgib/78huOP09vv

【问题讨论】:

    标签: laravel image url directory rename


    【解决方案1】:

    感谢大家的帮助。我设法通过编辑viewimage.blade.php解决了这个问题

    是的,当然需要在编辑文件后清除浏览器缓存。

    【讨论】:

      猜你喜欢
      • 2021-07-15
      • 2018-11-01
      • 1970-01-01
      • 2017-06-02
      • 2020-06-05
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多