0

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).

I am running a seed using faker which generates images and stores the images URL's in a table. 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

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:

<?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?

vitr
  • 6,766
  • 8
  • 30
  • 50
James
  • 2,800
  • 7
  • 45
  • 81
  • `http://localhost:8931/assets/images` i think this should be `http://localhost:8931/public/assets/images` – aldrin27 Jun 09 '16 at 09:41

2 Answers2

2

Change your call of URL::to with the helper function public_path() like this:

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

thefallen
  • 9,496
  • 2
  • 34
  • 49
  • Hi there. I changed it to your code (and understand why I should use that instead) but I still get the same error. Just to clarify, permissions on the folder are fine too. – James Jun 09 '16 at 09:52
  • Are you sure it's absolutely the same error? No different directory? No different exception? – thefallen Jun 09 '16 at 09:58
  • yes it's the same error: [InvalidArgumentException] Cannot write to directory "/var/www/public/assets/images" – James Jun 09 '16 at 10:39
  • Can you check if this is valid path **/var/www/public/assets/images** on your machine and you have rights to the folders? – thefallen Jun 09 '16 at 10:54
  • Yes it's a valid path. I run the DB:seed from inside a Vagrant box as that's where the DB connection is established and the application is hosted. This usually works fine. The permissions for the folder is james:www-data which is fine too. I'm so confused as to why this isn't working – James Jun 09 '16 at 10:57
  • Can you run **chmod -R 777 /var/www/public** to give all the folders full permissions and try again. – thefallen Jun 09 '16 at 11:06
  • Well, that seems to have worked. I think I might have not given 777 the first time I did it. – James Jun 09 '16 at 12:03
1

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.

Maybe you want to check if the process owner running the php script has write access to that location.

  • yeah I changed the function call to one that accesses a file path and I still get the same error. My permissions are ok too. – James Jun 09 '16 at 10:11