<分区>
我正在 Laravel 中构建一个电子商务应用程序,它需要保存来自不同页面的图像,例如添加产品、添加请求、更新配置文件等。我已经在 Controller 文件本身上为各个项目定义了图像存储位置.
UserController.php
$targetPath = '/Users/apple/Documents/eCommApp/storage/app/public/uploads/' . $user . '/img/profile/';
ProductController.php
$targetPath = '/Users/apple/Documents/eCommApp/storage/app/public/uploads/' . $user . '/img/product/';
我的问题是每次通过 git 提交新代码时我都需要不断更新远程服务器上的图像存储位置,因为它在本地和远程服务器上不同。
我的问题是:
我们能否在 config/中创建一个 constants.php,然后仅在那里定义所有路径,然后将此文件包含在 .gitignore 中,以便在推送代码时忽略此文件?
这是处理 Laravel 中图像存储位置的最佳(安全、高效)方式吗?
寻求您的建议,
谢谢
PS:这里是保存演出图片的代码。
if ($request->hasFile('ref_img')) {
if($request->file('ref_img')->isValid()) {
$types = array('_original.', '_150.', '_128.', '_64.', '_32.');
$sizes = array('150', '128', '64', '32');
$targetPath = '/Users/apple/Documents/eCommApp/storage/app/public/uploads/' . $user . '/img/gig/';
try {
$file = $request->file('ref_img');
$ext = $file->getClientOriginalExtension();
if ($gig->img == NULL){
$fName = time();
} else {
$fName = basename($gig->img, ".".$ext);
}
$o_name = $fName . array_shift($types) . $ext;
$original = Image::make($file->getRealPath());
$original->save($targetPath . $o_name);
foreach ($types as $key => $type) {
$newName = $fName . $type . $ext;
$newImg = Image::make($file->getRealPath());
$newImg->resize($sizes[$key], null, function ($constraint) {
$constraint->aspectRatio();
});
$newImg->save($targetPath . $newName);
}
$gig->img = 'storage/uploads/' . $user . '/img/gig/' . $fName . '.' . $ext;
} catch (Illuminate\Filesystem\FileNotFoundException $e) {
}
}
}