laravelforgecloud-hosting

Cannot write to images directory | cloud hosting with Laravel forge


I have a problem when deploying Laravel an application using Laravel forge. I try to generate fake images using faker package in Laravel but,

    Cannot write to directory "/home/forge/my.domain/public/storage/images/products/cover_img"

  at vendor/fakerphp/faker/src/Faker/Provider/Image.php:98
     94▕     ) {
     95▕         $dir = null === $dir ? sys_get_temp_dir() : $dir; // GNU/Linux / OS X / Windows compatible
     96▕         // Validate directory path
     97▕         if (!is_dir($dir) || !is_writable($dir)) {
  ➜ 98▕             throw new \InvalidArgumentException(sprintf('Cannot write to directory "%s"', $dir));
     99▕         }
    100▕ 
    101▕         // Generate a random filename. Use the server address so that a file
    102▕         // generated at the same time on a different server won't have a collision.

in the factory file,

'cover_img' => $this->faker->image(public_path('storage/images/products/cover_img'), 640, 480, null, false),

This is my first time using cloud hosting. when using shared hosting I can give permission to create a folder or can create a folder manually. please help me to solve this problem. Thank You!

***UPDATE ***

I change a few codes and again tried.

if(!File::exists(public_path().'/storage/images/products/cover_img'))
{   File::makeDirectory(public_path().'/storage/images/products/cover_img', 0777,true);
    }

Now it has error when deploy using Laravel forge,

ErrorException 

mkdir(): Permission denied

If someone can help me to solve this I really appreciate it.


Solution

  • Finally, I've figured out the issue, I think First I did the total wrong way because I tried to seed when deploying using forge with php artisan migrate:fresh --seed code. Then came that error. below has the wrong code.

    cd /home/forge/dev.mydomain.com
    git pull origin $FORGE_SITE_BRANCH
    $FORGE_COMPOSER install --no-interaction --prefer-dist --optimize-autoloader
    
    ( flock -w 10 9 || exit 1
        echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock
    
    if [ -f artisan ]; then
        $FORGE_PHP artisan migrate --seed
    fi
    

    So, finally, I did just deploy with default settings in the forge and I log in to serve using terminal, Then I run the php artisan migrate --seed command and I was successful without any error. This was the step I followed,

    1. Generate SSH key using git bash

    ssh-keygen

    1. Added ssh key into the forge
    2. run this command in git bash terminal

    ssh forge@ip address in the server

    1. then installed the oh my zsh using this code,

    sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

    1. then went to my directory in the host using terminal,

    cd dev.maydomain.com

    1. and final run seed,

    php artisan migrate --seed