当您将与现有图像同名的产品图像上传到 Magento 时,Magento 的正常行为是在新文件名的末尾附加一个“_1”。因此,如果我有一个名为“myFile.png”的图像并将第二个图像上传到具有相同名称的同一产品,Magento 会将其重命名为“myFile_1.png”。我想消除这种重命名并让同名文件替换现有文件。
我想我在以下位置找到了相关控制器:app/code/core/Mage/Adminhtml/controllers/Catalog/Product/GalleryController.php:
public function uploadAction()
{
try {
$uploader = new Mage_Core_Model_File_Uploader('image');
$uploader->setAllowedExtensions(array('jpg','jpeg','gif','png'));
$uploader->addValidateCallback('catalog_product_image',
Mage::helper('catalog/image'), 'validateUploadFile');
$uploader->setAllowRenameFiles(true);
$uploader->setFilesDispersion(true);
$result = $uploader->save(
Mage::getSingleton('catalog/product_media_config')->getBaseTmpMediaPath()
);
Mage::dispatchEvent('catalog_product_gallery_upload_image_after', array(
'result' => $result,
'action' => $this
));
/**
* Workaround for prototype 1.7 methods "isJSON", "evalJSON" on Windows OS
*/
$result['tmp_name'] = str_replace(DS, "/", $result['tmp_name']);
$result['path'] = str_replace(DS, "/", $result['path']);
$result['url'] = Mage::getSingleton('catalog/product_media_config')->getTmpMediaUrl($result['file']);
$result['file'] = $result['file'] . '.tmp';
$result['cookie'] = array(
'name' => session_name(),
'value' => $this->_getSession()->getSessionId(),
'lifetime' => $this->_getSession()->getCookieLifetime(),
'path' => $this->_getSession()->getCookiePath(),
'domain' => $this->_getSession()->getCookieDomain()
);
} catch (Exception $e) {
$result = array(
'error' => $e->getMessage(),
'errorcode' => $e->getCode());
}
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
}
我已经尝试将 setAllowedRenameFiles() 设置为 false。我也尝试过评论它。但这些似乎都没有给出预期的结果。
关于如何让 Magento 覆盖现有的同名文件有什么想法吗?