You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Prestashop 1.7中上传并保存文件?(ProductComments模块)

解决Prestashop 1.7 ProductComments模块文件上传问题

第一步:确保表单支持文件上传

在弹窗的评论表单模板(通常为modules/productcomments/views/templates/front/comment-form.tpl,自定义模板则修改对应文件)中,给form标签添加enctype="multipart/form-data"属性——浏览器只有识别该属性才会发送文件数据:

<form method="post" action="{$link->getModuleLink('productcomments', 'postComment')}" enctype="multipart/form-data">
    <!-- 原有表单字段 -->
    <input type="file" name="comment_attachment" id="comment_attachment" />
    <!-- 提交按钮等其他元素 -->
</form>

第二步:扩展目标控制器(避免修改原模块文件)

不要直接改动ProductComments模块的核心控制器,使用Prestashop的**控制器覆盖(Override)**机制:

  1. 在Prestashop根目录下创建路径/override/controllers/front/(不存在则新建)。
  2. 在该目录下创建文件ProductCommentsPostCommentModuleFrontController.php,内容如下:
<?php
class ProductCommentsPostCommentModuleFrontController extends ProductCommentsPostCommentModuleFrontControllerCore
{
    public function postProcess()
    {
        // 先执行原控制器逻辑,保证评论正常提交
        parent::postProcess();

        // 获取上传文件(name需与表单input的name一致)
        if (isset($_FILES['comment_attachment']) && $_FILES['comment_attachment']['error'] === UPLOAD_ERR_OK) {
            $uploadedFile = $_FILES['comment_attachment'];
            
            // 提取文件核心信息并存储到变量
            $fileName = $uploadedFile['name'];
            $fileTmpPath = $uploadedFile['tmp_name'];
            $fileSize = $uploadedFile['size'];
            $fileType = $uploadedFile['type'];

            // 文件验证示例:限制类型与大小
            $allowedTypes = ['image/jpeg', 'image/png', 'application/pdf'];
            $maxSize = 2 * 1024 * 1024; // 2MB
            if (!in_array($fileType, $allowedTypes) || $fileSize > $maxSize) {
                $this->errors[] = $this->trans('Invalid file type or size.', [], 'Modules.Productcomments.Shop');
                return;
            }

            // 移动文件到服务器指定目录
            $targetDir = _PS_UPLOAD_DIR_ . 'product_comments/';
            if (!file_exists($targetDir)) {
                mkdir($targetDir, 0777, true);
            }
            $targetFilePath = $targetDir . uniqid() . '_' . $fileName;
            if (move_uploaded_file($fileTmpPath, $targetFilePath)) {
                // 文件存储成功后,关联到当前评论记录(需提前创建对应关联表)
                $idComment = (int)Db::getInstance()->Insert_ID();
                if ($idComment) {
                    Db::getInstance()->execute(
                        'INSERT INTO `' . _DB_PREFIX_ . 'product_comment_attachment` (`id_comment`, `file_path`) VALUES (' . $idComment . ', "' . pSQL($targetFilePath) . '")'
                    );
                }
            }
        }
    }
}

第三步:更新缓存并测试

  1. 删除Prestashop缓存目录下的class_index.php(路径通常为/cache/class_index.php),让系统加载新的覆盖控制器。
  2. 测试表单提交,此时可通过$_FILES正常获取上传文件,且完成后续存储逻辑。

注意事项

  • 确保服务器目标上传目录(如示例中的/upload/product_comments/)有写入权限。
  • 若不想用Override机制,也可创建自定义模块,通过钩子actionProductCommentPost处理文件上传,但Override方式更直接。

内容的提问来源于stack exchange,提问作者be-1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 12:48:18