prestshop文件上传不工作


prestashop file upload won't work

我正在尝试在订单详细信息页面中实现文件上传。

创建表单

<form action="" method="post" id="uploadForm" enctype="multipart/form-data">
  <label for="fileUpload">{l s='File to upload:'}</label>
  <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
  <input type="file" name="fileUpload" id="fileUpload" />
<div class="submit">
    <input type="hidden" name="id_order" value="{$order->id|intval}" />
    <input type="submit" class="unvisible" name="submitMessage" value="{l s='Send'}"/>
    <button type="submit" name="submitMessage" class="button btn btn-default button-medium"><span>{l s='Send'}<i class="icon-chevron-right right"></i></span></button>
</div>
</form>

并将其添加到OrderDetailController.php

if (Tools::isSubmit('submitMessage')) {
   $idOrder = (int)Tools::getValue('id_order');
   $msgText = Tools::getValue('msgText');
   if (isset($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['name']) && !empty($_FILES['fileUpload']['tmp_name']))
   {
     $this->errors[] = Tools::displayError('Works');
   }
   ...

现在当我提交表单-它保存消息,完全忽略文件。

有人知道为什么吗?

我自己解决了。在这里回答

是ajax问题,没有发送$_FILES[] data

我假设你把它放在默认模板的order-detail.tpl中。

您将文件输入放到一个单独的表单中,该表单不做任何事情,因为它没有提交按钮。该页面的评论表单是一个完全不同的表单,这就是为什么你的输入被忽略的原因。

你要做的是把你的html代码没有<form>标签到一个适当的形式提交消息。表格在order-detail.tpl的底部

<form action="{$link->getPageLink('order-detail', true)|escape:'html':'UTF-8'}" method="post" class="std" id="sendOrderMessage">
    <h3 class="page-heading bottom-indent">{l s='Add a message'}</h3>
    <p>{l s='If you would like to add a comment about your order, please write it in the field below.'}</p>
    <p class="form-group">
        <label for="id_product">{l s='Product'}</label>
        <select name="id_product" class="form-control">
            <option value="0">{l s='-- Choose --'}</option>
            {foreach from=$products item=product name=products}
                <option value="{$product.product_id}">{$product.product_name}</option>
            {/foreach}
        </select>
    </p>
    <p class="form-group">
        <textarea class="form-control" cols="67" rows="3" name="msgText"></textarea>
    </p>
    <!-- Your html snippet -->
    <p class="form-group">
        <label for="fileUpload">{l s='File to upload:'}</label>
        <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
        <input type="file" name="fileUpload" id="fileUpload" />
    </p>
    <div class="submit">
        <input type="hidden" name="id_order" value="{$order->id|intval}" />
        <input type="submit" class="unvisible" name="submitMessage" value="{l s='Send'}"/>
        <button type="submit" name="submitMessage" class="button btn btn-default button-medium"><span>{l s='Send'}<i class="icon-chevron-right right"></i></span></button>
    </div>
</form>