BrownieCoffee's avatar

upload image in specific folder with faker

Hi,

I'm using faker and seeder to generates fake datas and I would like to upload image in specific folder for my users's projects.

actually my code follow is giving me error Cannot write to directory "/var/www/html/goshr/storage/projets/Kelley/projet_385093/thumbnail/"

$factory->define(Project::class, function (Faker $faker) {
  
    $user = factory(App\User::class)->create([
        'is_admin' => 0
    ]);

    $categories = App\Category::pluck('id')->toArray();
    $uOM = App\UnityOfMeasurement::pluck('id')->toArray();
    $difficulties = App\DifficultyLevel::pluck('id')->toArray();
    $status = App\Status::pluck('id')->toArray();

    session(['project_identifier' => mt_rand(100000, 999999)]);
    $identifier = session('project_identifier');

        $storagePath = 'projets/' . $user->username.'/projet_' . $identifier . '/thumbnail/';

        if (!Storage::exists($storagePath)) {
            // ... je récupère mon image que je vais stoker dans le dossier avatars/[nom-de-l'utilisateur] dans le storage local 'public/'
            $storagePath;
        }



    // $faker->image('public/storage/projets/'.$user->username.'/projet_'.$identifier.'/thumbnail/',640,480, null, false),
    return [
        'user_id' => $user->id,
        'title' => $faker->sentence($nbWords = 6, $variableNbWords = true),
        'category_id' => $faker->randomElement($categories),
        'id_number' => $identifier ,
        'duration' => mt_rand(1,24),
        'budget' => mt_rand(1,100),
        'unity_of_measurement_id' => $faker->randomElement($uOM),
        'difficulty_level_id' => $faker->randomElement($difficulties),
        'status_id' => $faker->randomElement($status),
        'thumbnail' => $faker->image(storage_path('projets/'.$user->username.'/projet_'.$identifier.'/thumbnail/'),640,480, null, false),
        'content' => $faker->text($maxNbChars = 200),
    ];
});

my folder permission is okay.

How can I do ?

Thank you by advance. S.U.+

0 likes
7 replies
BrownieCoffee's avatar

@snapey this following code create project folder if it doesn't exist.

        // $storagePath = 'projets/' . $user->username.'/projet_' . $identifier . '/thumbnail/';

if (!Storage::exists($storagePath)) {
    $storagePath;
}

I already used public path and relative path and it fails for both.

Sinnbeck's avatar

Nope this code does nothing.. It checks if the path exists and does nothing :)

if (!Storage::exists($storagePath)) { //check if path exists
    $storagePath; // Do nothing. Just writing a variable does nothing :)
}
BrownieCoffee's avatar

@sinnbeck Hi yes thanks I correct that but It still fails.


  $storagePath = 'projets/' . $user->username.'/projet_' . $identifier . '/thumbnail';

        
        if (!Storage::exists($storagePath)) {
            // ... je récupère mon image que je vais stoker dans le dossier avatars/[nom-de-l'utilisateur] dans le storage local 'public/'
            Storage::makeDirectory('public/'.$storagePath);
        }
Snapey's avatar

What about the solution I already proposed. Change the last parameter on the faker command from false to true to tell it that you are providing the full path and not a relative path.

MaverickChan's avatar

@browniecoffee seems like you wanna save the file into public folder,

1 you should check your permission

if (!file_exists($path)) {

    	mkdir($path,0777,true);
}

2 use a full path instead of a relative path , put a '/` in front

$storagePath = '/projets/' . $user->username.'/projet_' . $identifier . '/thumbnail/';

Please or to participate in this conversation.