php - 定义图像存储位置的最佳实践

标签 php git laravel

<分区>

我正在 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 提交新代码时我都需要不断更新远程服务器上的图像存储位置,因为它在本地和远程服务器上不同。

我的问题是:

  1. 我们能否在 config/中创建一个 constants.php,然后仅在那里定义所有路径,然后将此文件包含在 .gitignore 中,以便在推送代码时忽略此文件?

  2. 这是处理 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) {
            }
        }
    }

最佳答案

使用helpers喜欢public_path() :

$targetPath = public_path($user . '/img/profile/');

这个助手会在你的 Laravel 项目中生成一个完整的路径到 public 目录。

关于php - 定义图像存储位置的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48646432/

相关文章:

php - 悬停在 Div 上时查询 MySql

php - 在 Windows 上执行 Git 钩子(Hook)

git - "Secret text"Git 凭据未显示在 Jenkins 项目源代码管理部分

laravel - 更改 Laravel Migrations 的执行顺序

php - 随机出现的 gzip header

php - 拉维尔 5.2 : How to get the role of current user when using `Zizaco/entrust` ?

php - Yii 框架 : role based access control

PHP URL 编码/解码表单字段的 %u2019 中的漂亮引号

php - 如何从 GitHub 推送到你的 VPS

svn - 将现有的 Git 存储库推送到 SVN