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

PrestaShop1.6后台维护控制器添加图片上传字段无上传解决咨询

Hey there! Let's tackle this file upload issue in your PrestaShop 1.6 AdminMaintenanceController rewrite. I've run into similar problems before, so here are the key fixes you'll need to implement to get the upload working:

1. Ensure the form uses the correct enctype

File uploads require the form to have enctype="multipart/form-data"—PrestaShop's default maintenance form doesn't include this, so we need to add it to our rewritten controller:

public function __construct() {
    parent::__construct();
    $this->bootstrap = true;
    
    // Add enctype to the form attributes
    $this->fields_form[0]['form']['attributes'] = array('enctype' => 'multipart/form-data');
    
    // Add your file upload field to the existing form inputs
    $this->fields_form[0]['form']['input'][] = array(
        'type' => 'file',
        'label' => $this->l('Maintenance Image'),
        'name' => 'maintenance_image',
        'desc' => $this->l('Upload an image to display on the maintenance page.'),
        'display_image' => true,
        'image' => Configuration::get('MAINTENANCE_IMAGE') ? _PS_IMG_URL_ . 'maintenance/' . Configuration::get('MAINTENANCE_IMAGE') : false
    );
}

2. Add upload handling logic in postProcess()

The field shows up, but we need to process the actual file upload when the form is submitted. Add this to your controller:

public function postProcess() {
    parent::postProcess();
    
    // Only run this when the form is submitted
    if (Tools::isSubmit('submitAdd' . $this->table)) {
        if (isset($_FILES['maintenance_image']) && $_FILES['maintenance_image']['error'] == UPLOAD_ERR_OK) {
            // Define target directory (create it if it doesn't exist)
            $target_dir = _PS_IMG_DIR_ . 'maintenance/';
            if (!file_exists($target_dir)) {
                mkdir($target_dir, 0777, true);
            }
            
            // Validate file type (optional but recommended)
            $allowed_extensions = array('jpg', 'jpeg', 'png', 'gif');
            $file_ext = strtolower(pathinfo($_FILES['maintenance_image']['name'], PATHINFO_EXTENSION));
            if (!in_array($file_ext, $allowed_extensions)) {
                $this->errors[] = $this->l('Only JPG, PNG, and GIF images are allowed.');
                return;
            }
            
            // Generate a unique filename to avoid overwrites
            $file_name = uniqid('maint_') . '.' . $file_ext;
            $target_path = $target_dir . $file_name;
            
            // Move the uploaded file to the target directory
            if (move_uploaded_file($_FILES['maintenance_image']['tmp_name'], $target_path)) {
                // Delete old image (optional cleanup)
                $old_image = Configuration::get('MAINTENANCE_IMAGE');
                if ($old_image && file_exists($target_dir . $old_image)) {
                    unlink($target_dir . $old_image);
                }
                // Save the new image filename to PrestaShop configuration
                Configuration::updateValue('MAINTENANCE_IMAGE', $file_name);
            } else {
                $this->errors[] = $this->l('Failed to upload the image. Check if the img/maintenance directory has write permissions.');
            }
        }
    }
}

3. Pass the image to the maintenance template

To display the image in maintenance.tpl, we need to assign the image URL to the Smarty context. You can do this by overriding the MaintenanceFrontController:

class MaintenanceFrontController extends MaintenanceFrontControllerCore {
    public function initContent() {
        parent::initContent();
        // Assign the image URL to Smarty
        $image_url = Configuration::get('MAINTENANCE_IMAGE') ? _PS_IMG_URL_ . 'maintenance/' . Configuration::get('MAINTENANCE_IMAGE') : '';
        $this->context->smarty->assign('maintenance_image', $image_url);
    }
}

Then update themes/your-theme/templates/maintenance.tpl to display the image:

{if $maintenance_image}
    <div class="maintenance-image">
        <img src="{$maintenance_image}" alt="Maintenance Image" />
    </div>
{/if}

4. Verify directory permissions

Make sure the img/maintenance directory on your server has write permissions (usually 777 or 755, depending on your server setup). If the web server can't write to this directory, the upload will fail.

Full Rewritten Controller Example

Putting it all together, your AdminMaintenanceController should look like this:

class AdminMaintenanceController extends AdminMaintenanceControllerCore {
    public function __construct() {
        parent::__construct();
        $this->bootstrap = true;
        
        // Update form enctype
        $this->fields_form[0]['form']['attributes'] = array('enctype' => 'multipart/form-data');
        
        // Add upload field
        $this->fields_form[0]['form']['input'][] = array(
            'type' => 'file',
            'label' => $this->l('Maintenance Image'),
            'name' => 'maintenance_image',
            'desc' => $this->l('Upload an image to display on the maintenance page.'),
            'display_image' => true,
            'image' => Configuration::get('MAINTENANCE_IMAGE') ? _PS_IMG_URL_ . 'maintenance/' . Configuration::get('MAINTENANCE_IMAGE') : false
        );
    }
    
    public function postProcess() {
        parent::postProcess();
        
        if (Tools::isSubmit('submitAdd' . $this->table)) {
            if (isset($_FILES['maintenance_image']) && $_FILES['maintenance_image']['error'] == UPLOAD_ERR_OK) {
                $target_dir = _PS_IMG_DIR_ . 'maintenance/';
                if (!file_exists($target_dir)) {
                    mkdir($target_dir, 0777, true);
                }
                
                $allowed_extensions = array('jpg', 'jpeg', 'png', 'gif');
                $file_ext = strtolower(pathinfo($_FILES['maintenance_image']['name'], PATHINFO_EXTENSION));
                if (!in_array($file_ext, $allowed_extensions)) {
                    $this->errors[] = $this->l('Only JPG, PNG, and GIF images are allowed.');
                    return;
                }
                
                $file_name = uniqid('maint_') . '.' . $file_ext;
                $target_path = $target_dir . $file_name;
                
                if (move_uploaded_file($_FILES['maintenance_image']['tmp_name'], $target_path)) {
                    $old_image = Configuration::get('MAINTENANCE_IMAGE');
                    if ($old_image && file_exists($target_dir . $old_image)) {
                        unlink($target_dir . $old_image);
                    }
                    Configuration::updateValue('MAINTENANCE_IMAGE', $file_name);
                } else {
                    $this->errors[] = $this->l('Failed to upload the image. Check directory permissions.');
                }
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Owake

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:41:56