【问题标题】:How to correct public_path() in Laravel?如何更正 Laravel 中的 public_path()?
【发布时间】:2021-09-01 10:25:16
【问题描述】:

我想使用 Laravel 在数据库中保存多张图片。我在 public_path() 中出现错误提示

(消息:“找不到类 'Gallerie_img_client'”)

这是我的代码:

 public static function uploadImage(Request $request, $user)
    {
        DB::beginTransaction();
        try {

            $current_timestamp = Carbon::now()->timestamp; // Produces something like 1552296328
            $emitter = User::where('id', $user)->where('active', true)->first();
            $haseFile = $request->input('hasFile');
            $idClient = $request->input('idClient');
            if ($haseFile) {
                foreach ($request->input('files') as $key => $file) {
                    $aa = explode(';', $file['file']);
                    $aa1 = explode('/', $aa[0]);
                    $extension = $aa1[1];
                    $fileName = $current_timestamp . $key . '.' . $extension;
                    if (strpos($file['file'], ',') !== false) {
                        @list($encode, $file['file']) = explode(',', $file['file']);
                    }
                    Gallerie_img_client::insert([
//                        'id'=> '1',
                        'name'=>$file['name'],
                        'url'=>$fileName,
                        'size'=>$file['size'],
                        'idClient'=>$idClient,
                    ]);

                    /*** Save file in folder public/data/Images ***/
              \Gallerie_img_client::put(public_path() . '/data' . '/' . '/Images' . '/' . == >causing me error 

$fileName, base64_decode($file['file']));
                    // Storage::put($fileName, $file);
                }
            }

【问题讨论】:

  • 问题在于您正在使用的类 Gallerie_img_client 尝试导入它
  • 我做到了!但它在这一行中不起作用“\Gallerie_img_client::put(public_path() . '/data' ...”
  • 您是否尝试过像这样导入它:使用 path/to/Gallerie_img_client; ?
  • 尝试删除行中的斜线
  • @HananAlhasan 我收到了这个错误(未定义的方法 App\Gallerie_img_client::put())

标签: laravel laravel-7


【解决方案1】:

您必须使用 Storage 类将文件保存在 public_path() 或 storage_path() 中

Storage::disk('public')->put($filePath, (string) $image, 'public');

config/filesystens.php 中,您将看到如下代码,该代码用于 Storage 类的磁盘功能。您可以根据需要创建自己的。

'public' => [
    'driver' => 'local',
    'root' => storage_path('app/public'),
    'url' => env('APP_URL').'/storage',
    'visibility' => 'public',
],

使用Storage disk public方法将文件存储在storage/app/public文件夹中

您必须在公用文件夹中创建快捷链接以进行存储。您可以运行此php artisan storage:link 来创建链接。

【讨论】:

    猜你喜欢
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多