简体   繁体   English

将静态内容保存到Laravel公用文件夹中

[英]Saving static content into the Laravel public folder

I am having trouble storing images in the 'public' folder of the Laravel framework (which I believe is the static content folder? Please correct me if I am wrong). 我在将图像存储在Laravel框架的“ public”文件夹中时遇到麻烦(我认为这是静态内容文件夹?如果我写错了,请纠正我)。

I am running a seed using faker which generates images and stores the images URL's in a table. 我正在使用fakerr运行种子,该种子生成图像并将图像URL存储在表中。 This is all working correctly but the seed itself keeps failing as it can't seem to access or find the folder within the public folder I have created. 一切正常,但是种子本身一直失败,因为它似乎无法访问或在我创建的公用文件夹中找到该文件夹​​。 This is my public folder structure: 这是我的公用文件夹结构:

/public/assets/images / public / assets / images

And here is my seed which should save my images into this folder: 这是我的种子,应将我的图像保存到此文件夹中:

<?php

use Illuminate\Database\Seeder;

class ProductsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('products')->truncate();

        $faker = Faker\Factory::create();

        $limit = 30;

        for($i = 0; $i < $limit; $i++) {
            DB::table('products')->insert([
                'title'       => $faker->name,
                'SKU'         => $faker->name,
                'description' => $faker->text,
                'created_at'  => $faker->dateTime,
                'updated_at'  => $faker->dateTime,
                'images'      => $faker->image(URL::to(Config::get('assets.images')) ,800, 600, [], [])
            ]);
        }
    }
}#

The 'assets.images' config file is: “ assets.images”配置文件为:

<?php
    return [
        'images' => '/assets/images'   
    ];

When I try and run the seed to populate my database I get the following error: 当我尝试运行种子填充数据库时,出现以下错误:

  [InvalidArgumentException]                                       
  Cannot write to directory "http://localhost:8931/assets/images"  

I cannot see where I am going wrong. 我看不到我要去哪里错了。 Can anyone offer any insight? 谁能提供任何见解?

使用辅助函数public_path()更改对URL :: to的调用,如下所示:

$faker->image(public_path(Config::get('assets.images')) ,800, 600, [], [])

I'm not a laravel expert, but the thrown exception looks like you're trying to write to an url rather than a file path on the disk. 我不是laravel专家,但是抛出的异常看起来像是您尝试写入url而不是磁盘上的文件路径。

Maybe you want to check if the process owner running the php script has write access to that location. 也许您想检查运行php脚本的进程所有者是否对该位置具有写权限。

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

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