【问题标题】:Cannot write to images directory | cloud hosting with Laravel forge无法写入图像目录 |使用 Laravel forge 进行云托管
【发布时间】:2022-02-08 07:03:39
【问题描述】:

我在使用 Laravel forge 部署 Laravel 应用程序时遇到问题。我尝试在 Laravel 中使用 faker 包生成假图像,但是,

    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.

在工厂文件中,

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

这是我第一次使用云托管。使用共享主机时,我可以授予创建文件夹的权限,也可以手动创建文件夹。请帮我解决这个问题。谢谢!

***更新***

我更改了一些代码并再次尝试。

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

现在使用 Laravel forge 部署时出现错误,

ErrorException 

mkdir(): Permission denied

如果有人能帮我解决这个问题,我真的很感激。

【问题讨论】:

    标签: laravel forge cloud-hosting


    【解决方案1】:

    最后,我已经解决了这个问题,我认为首先我做错了,因为我在使用 forge 和 php artisan migrate:fresh --seed 代码进行部署时尝试播种。然后出现了那个错误。下面有错误的代码。

    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
    

    所以,最后,我只是在 forge 中使用默认设置进行部署,然后使用终端登录服务,然后运行 ​​php artisan migrate --seed 命令,我成功了,没有任何错误。这是我遵循的步骤,

    1. 使用 git bash 生成 SSH 密钥

    ssh-keygen

    1. 在伪造中添加了 ssh 密钥
    2. 在 git bash 终端中运行此命令

    服务器中的ssh forge@ip地址

    1. 然后使用此代码安装 oh my zsh,

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

    1. 然后使用终端进入我在主机中的目录,

    cd dev.maydomain.com

    1. 和最终运行种子,

    php artisan migrate --seed

    【讨论】:

      猜你喜欢
      • 2018-04-30
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 2012-04-14
      • 1970-01-01
      • 2021-07-28
      • 2016-04-22
      • 2021-08-13
      相关资源
      最近更新 更多