gpt4 book ai didi

php - 无法写入目录 - Digital Ocean

转载 作者:行者123 更新时间:2023-12-04 22:11:04 26 4
gpt4 key购买 nike

我正在尝试上传带有发布请求的图像并将其移动到目录中。我试过..

$file = $request->file('file');
$extension = strtolower($file->getClientOriginalExtension());

$finfo = finfo_open(FILEINFO_MIME_TYPE);

$generatedName = sha1(time().time());
$directory = '../public/uploads/imgs/'; // works
$fileName = $generatedName . "." . $extension; // works

// 1
$file->move($directory, $fileName);
// 2
$file->move(base_path().$directory, $fileName);
此时,我收到错误消息:

1 Unable to write in the "../public/uploads/imgs" directory

2 Unable to write in the "var/www/laravel/public/uploads/imgs" directory



我以为是权限引起的,但也没有帮助..我试过了
sudo chmod 770 /var/www/laravel/public/uploads (also /imgs/
and
sudo chmod -R 775 /var/www/laravel/public/uploads
我还发现了一些其他的尝试,但无法弄清楚在用户名位中写什么:
sudo chown username:www-data /var/www/laravel/public/uploads/

我在 Digital Ocean 上使用 NGINX

最佳答案

从您的错误消息开始:

1.无法写入“../public/uploads/imgs”目录

默认情况下,您的 nginx 指向 public Laravel 安装文件夹。 ../public/uploads/imgs因此将解析为 /var/www/laravel/public/uploads/imgs如果它没有对 laravel 的读取权限,这将不起作用文件夹。

通常,nginx 应该只有对您的 public 的读访问权限文件夹。

2.无法写入“var/www/laravel/public/uploads/imgs”目录

路径是相对的。它的绝对路径是 /var/www/laravel/public/var/www/laravel/public/uploads/imgs默认情况下不存在。

解决方案:

替换这些行:

$directory = '../public/uploads/imgs/';
$file->move(base_path().$directory, $fileName);

和:
$directory = 'uploads/imgs/';
$file->move(public_path().$directory, $fileName);

文档: https://laravel.com/docs/5.1/helpers#paths

警告:不要给你的 public nginx 写权限文件夹!如果有人设法上传并覆盖您的 index.php ,您的网站将受到损害。

关于php - 无法写入目录 - Digital Ocean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36733306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com