Magento 自定义图像上传不起作用

发布于 2024-12-14 06:25:34 字数 2065 浏览 6 评论 0原文

我需要为 Magento 博客模块(AW 博客)中的帖子创建一个图像上传字段。 因此,每个帖子都需要包含一张图片。

我编辑了以下文件: /home/clients/websites/w_gale/public_html/gale/app/code/community/AW/Blog/Block/Manage/Blog/Edit/Tab/Form.php

添加了这样的字段集:

$fieldset->addField('fileinputname', 'image', array(
            'label'     => Mage::helper('blog')->__('Upload image'),
            'required'  => false,
            'name'      => 'fileinputname',
            'required'  => true,
            'style'     => 'height:100px;border:2px solid #999;',
    )); 

在此文件顶部的右侧地方,我定义了这样的形式:

$form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
            'method' => 'post',
            'enctype' => 'multipart/form-data',
       )
    );

在 Blogcontroller.php 中,我添加了以下代码,位于 if ($data = $this->getRequest()->getPost()) 下方{ line

if(isset($_FILES['fileinputname']['name']) && (file_exists($_FILES['fileinputname']['tmp_name']))) {
            try {

                $uploader = new Varien_File_Uploader('fileinputname');

                $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything

                $uploader->setAllowRenameFiles(true);
                $uploader->setFilesDispersion(false);

                $path = Mage::getBaseDir('media') . DS ;

                $uploader->save($path, $_FILES['fileinputname']['name']);

                $data['imageurl'] = $_FILES['fileinputname']['name'];

            } catch(Exception $e) {

            }
        } else {       

            if(isset($data['fileinputname']['delete']) && $data['fileinputname']['delete'] == 1)
                $data['imageurl'] = '';
            else
                unset($data['fileinputname']);
        }

但是,上传不起作用。我做错了什么? 我在数据库中的适当字段中添加了一个特殊行。 当我手动输入数据库值时,前端部分会显示数据库值。

谢谢

I need to create an image upload field for posts in Magento Blog Module (AW blog).
Therefore, each post needs to contain an image.

I edited the following files:
/home/clients/websites/w_gale/public_html/gale/app/code/community/AW/Blog/Block/Manage/Blog/Edit/Tab/Form.php

added fieldset like this:

$fieldset->addField('fileinputname', 'image', array(
            'label'     => Mage::helper('blog')->__('Upload image'),
            'required'  => false,
            'name'      => 'fileinputname',
            'required'  => true,
            'style'     => 'height:100px;border:2px solid #999;',
    )); 

on top of this file, in the right place, I defined form like this:

$form = new Varien_Data_Form(array(
            'id' => 'edit_form',
            'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
            'method' => 'post',
            'enctype' => 'multipart/form-data',
       )
    );

In Blogcontroller.php I added the following code, just bellow the if ($data = $this->getRequest()->getPost()) { line

if(isset($_FILES['fileinputname']['name']) && (file_exists($_FILES['fileinputname']['tmp_name']))) {
            try {

                $uploader = new Varien_File_Uploader('fileinputname');

                $uploader->setAllowedExtensions(array('jpg','jpeg','gif','png')); // or pdf or anything

                $uploader->setAllowRenameFiles(true);
                $uploader->setFilesDispersion(false);

                $path = Mage::getBaseDir('media') . DS ;

                $uploader->save($path, $_FILES['fileinputname']['name']);

                $data['imageurl'] = $_FILES['fileinputname']['name'];

            } catch(Exception $e) {

            }
        } else {       

            if(isset($data['fileinputname']['delete']) && $data['fileinputname']['delete'] == 1)
                $data['imageurl'] = '';
            else
                unset($data['fileinputname']);
        }

However, the upload doesn't work. What am I doing wrong?
I added a special row in appropriate field in a database.
The frontend section displays the database value when I enter it manually.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2024-12-21 06:25:34

此方法的代码来自上传图像的数据助手。您需要自己实现方法 getBaseDir() (它返回您希望存储上传文件的目录)。

/**
 * Upload image and return uploaded image file name or false
 *
 * @throws Mage_Core_Exception
 * @param string $scope the request key for file
 * @return bool|string
 */
public function uploadImage($scope)
{
    $adapter  = new Zend_File_Transfer_Adapter_Http();
    $adapter->addValidator('ImageSize', true, $this->_imageSize);
    $adapter->addValidator('Size', true, $this->_maxFileSize);
    if ($adapter->isUploaded($scope)) {
        // validate image
        if (!$adapter->isValid($scope)) {
            Mage::throwException(Mage::helper('mycompany_mymodule')->__('Uploaded image is not valid'));
        }
        $upload = new Varien_File_Uploader($scope);
        $upload->setAllowCreateFolders(true);
        $upload->setAllowedExtensions(array('jpg', 'gif', 'png'));
        $upload->setAllowRenameFiles(true);
        $upload->setFilesDispersion(false);
        if ($upload->save($this->getBaseDir())) {
            return $upload->getUploadedFileName();
        }
    }
    return false;
}

This code of method from data helper which uploads image. You need implement method getBaseDir() (which returns dir where you wish store your uploaded files) by yourself.

/**
 * Upload image and return uploaded image file name or false
 *
 * @throws Mage_Core_Exception
 * @param string $scope the request key for file
 * @return bool|string
 */
public function uploadImage($scope)
{
    $adapter  = new Zend_File_Transfer_Adapter_Http();
    $adapter->addValidator('ImageSize', true, $this->_imageSize);
    $adapter->addValidator('Size', true, $this->_maxFileSize);
    if ($adapter->isUploaded($scope)) {
        // validate image
        if (!$adapter->isValid($scope)) {
            Mage::throwException(Mage::helper('mycompany_mymodule')->__('Uploaded image is not valid'));
        }
        $upload = new Varien_File_Uploader($scope);
        $upload->setAllowCreateFolders(true);
        $upload->setAllowedExtensions(array('jpg', 'gif', 'png'));
        $upload->setAllowRenameFiles(true);
        $upload->setFilesDispersion(false);
        if ($upload->save($this->getBaseDir())) {
            return $upload->getUploadedFileName();
        }
    }
    return false;
}
椒妓 2024-12-21 06:25:34

您使用的是正确的代码。我通过使用正确的 MYSQL 数据类型解决了这个问题。当我将数据类型从“text”更改为“varchar(255)”时,它解决了问题

并且...确保添加以下代码:

$form = new Varien_Data_Form(array(
        'id' => 'edit_form',
        'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
        'method' => 'post',
        'enctype' => 'multipart/form-data',
   )
);

在 /app/code/community/AW/Blog/Block/Manage/ 中博客/编辑/Form.php
不是:应用程序/代码/社区/AW/博客/块/管理/博客/编辑/选项卡/Form.php

You're using the right code. I solved the problem by using the right MYSQL data type. When I changed the data type from 'text' to 'varchar(255)' it solved the problem

And ... make sure that you add the following code:

$form = new Varien_Data_Form(array(
        'id' => 'edit_form',
        'action' => $this->getUrl('*/*/save', array('id' => $this->getRequest()->getParam('id'))),
        'method' => 'post',
        'enctype' => 'multipart/form-data',
   )
);

in /app/code/community/AW/Blog/Block/Manage/Blog/Edit/Form.php
NOT: app/code/community/AW/Blog/Block/Manage/Blog/Edit/Tab/Form.php

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文