0

我正在用 cakephp3.7 构建一个电子商务网站,对于卖家发布他们的产品,他们需要上传要销售的文章图片,并且通过带有 Blob 列的表单。图像从视图上传得很好,但在数据库中,图像的真实名称没有出现,取而代之的是,在那个地方,所有图像的名称都是所有文章的“Blob”。现在的问题是,当我尝试从数据库中检索文章的图像(仅通过它的名称知道)到控制器以便将其发送到网站的前端时,它找不到图像,我我被严重卡住了!!!

但是当我到达 webroot/img 文件夹时,我找到了上传的图像,当我在前端视图中手动复制/粘贴名称时,图像会与文章的其他属性一起出现在前端。

这是应该从数据库中查找文章及其图像的控制器代码,要寻址的表是表“annonces”,存储图像的表的字段是“piece_jointe”:

public function articles()
        {
            $this->viewBuilder()->layout('react2019');
            $annonces = $this->Annonces->find()
            ->where(['Annonces.subcategorie_id'=>1])
            ->contain(['Comptes', 'Subcategories'])
            ->all();


            $this->set(compact('annonces'));
        }

这是将发布的文章转发到前端时的文章视图,我现在无法检索的字段是“piece_jointe”:

<?php foreach ($annonces as $annonce): ?> 
  <div class="col-lg-4 col-md-6 mb-4">
            <div class="card h-100">
              <a href="#"><?= $this->Html->image($annonce->piece_jointe, array('height'=>90, 'width'=>90)); ?></a>

              <div class="card-body">
                <h4 class="card-title">
                  <a href="#"><?= h($annonce->annonce_titre) ?></a>
                </h4>
                <h5><?= h($annonce->prix_produit) ?></h5>
                <p class="card-text"><?= h($annonce->annonce_description) ?></p>
              </div>
              <div class="card-footer">
                <small class="text-muted">&#9733; &#9733; &#9733; &#9733; &#9734;</small>
              </div>
            </div>
          </div>
<?php endforeach; ?>

我想知道从 mysql 数据库中检索 Blob 类型“piece_jointe”的名称并在 cakephp 语法中使用它的方法:

<?= $this->Html->image($annonce->piece_jointe, array('height'=>90, ' 
        width'=>90)); ?>

因为当我将 $annonce->piece_jointe 变量替换为它的真实名称“macbook”时,这可以正常工作,例如:

<?= $this->Html->image('macbook', array('height'=>90, 'width'=>90)); ?>
4

1 回答 1

0

到目前为止,一切都很好 !我深入研究了 cakephp 关于处理图像、视频、pdf、文档等文件的约定。我理解了更多的东西。首先,我认为我应该将图像作为 BLOB 存储在数据库中,以尊重 MVC 概念。但是我终于发现 cakephp 在处理图像和文件方面有一个优势,因为图像和文件是敏感信息,会在应用程序中打开漏洞,而 cakephp 建议不要将图像直接存储在数据库中,以防止诸如SQL之类的攻击注射。很多人问过 cakephp 中如何处理图片的问题,但给出的答案并不完全适用。因此,我想通过 4 个步骤提供要点: 1- 当我们上传文件时,就像我对专栏所做的那样'piece_jointe'我从 'BLOB' 更改为'VARCHAR' 的类型, cakephp 为该图像提供参数,正如您从我的 debug($this->request->data) 的渲染中看到的那样:

[
    'title' => 'computer',
    'category_id' => '1',
    'subcategory_id' => '3',
    'announce_description' => 'smart price',
    'prix_produit' => '500000',
    'photo'=> '',
    'compte_id' => '1',
    'piece_jointe' => [
        'tmp_name' => '/private/var/folders/kl/lgxkfn5x4r1fjht2wckngjzr0000gn/T/phpodWgh1',
        'error' => (int) 0,
        'name' => 'macbook.jpg',
        'type' => 'image/jpeg',
        'size' => (int) 37780
    ]
]

您可以观察到 'piece_jointe' 具有由 cakephp 创建的'temp_name'、'error'、'name'、'type'、'size'等字段,供我们自定义图像和文件上传。但是我们在这堆信息中需要的是“名称”。所以我们要在数据库中的表announce 中添加另一个varchar 列,这就是为什么我添加了varchar 类型的列'photo',你可以在上面的调试结果中看到。目的是为“照片”提供图像的路径 $uploadPath = WWW_ROOT.'img/products/' 及其名称 $fileName = $this->request->data['piece_jointe']['name' ] 在我们的控制器 add() 方法中,因此 cakephp 可以遵循该路径并从相关文件夹中获取图像,在我的案例中,该文件夹位于 webroot/img 中的产品文件夹中。整体操作如下所示:

    if (!empty ($this->request->data['piece_jointe']['tmp_name']))
                            {
                                  $fileName = $this->request->data['piece_jointe']['name'];
                                  $uploadPath = WWW_ROOT.'img/products/';
                                  $uploadFile = $uploadPath.$fileName;
                                  if (move_uploaded_File($this->request->data['piece_jointe']['tmp_name'], $uploadFile))
                                    {  
                                        $annonce->photo = 'products/'.$fileName;                

                                    }

                             }

2- 然后在模型/表中不要忘记将其添加到公共函数 initialize(array $config) 方法中:

$this->addBehavior('时间戳');

另一件事也不要忘记在 add() 视图中提到上传“piece_jointe”是“type”=>“file”的图像的位置。你可能有这样的:

       <?php
            echo $this->Form->input('piece_jointe', [ 'type' => 'file']); 
        ?>
</Div>

3-最后,当我发疯时,“照片”列在数据库/products/macbook.jpg 中收到了这个值,而不是“BLOB”。4- 要检索图像,我们只需将其添加到视图中:

<?= $this->Html->image($annonce->photo, array('height'=>90, 'width'=>90)); ?>

总之,这些是在 cakephp 中处理图像和文件上传的可靠步骤。将图像存储到具有 BLOB 类型的数据库中并期望它显示在前端 frow 一个原始的 MVC 概念实在是太循环了。还要感谢@ndm 让我知道我错过了这个概念。

于 2019-08-13T13:17:36.697 回答