上传文件(附件)错误

时间:2017-05-30 03:53:15

标签: php extjs file-upload zend-framework2

保存到form和datagrid时出错 我不知道,错误是什么? 我使用extjs 4和zend framework 2

这是邮件错误:

sourceClass: "Ext.JSON", 
sourceMethod: "decode", 
msg: "You're trying to decode an invalid JSON String: Wa…session":true,
"other":null,"processingTime":null}", 
message: "You're trying to decode an invalid JSON String: Wa…session":true,
"other":null,"processingTime":null}"}
message: "You're trying to decode an invalid JSON String: 
Warning: Invalid argument supplied for foreach() in Purchase.php on line 331↵
{"success":false,"data":{"no_po":null,"date_po":null,"supplier":null,"shipping_to":null,"total_price_head":null,"vat":null,"net_price":null,"id_po":null,"file_ext":"png","file_type":"image\/png","file_size":633237,"file_name":"2015-02-23-Negative-World-left-hand.png"},"messages":[],"pageSize":null,"start":null,"total":null,"session":true,"other":null,"processingTime":null}"
msg: "You're trying to decode an invalid JSON String: 
Warning: Invalid argument supplied for foreach() in Purchase.php on line 331↵
{"success":false,"data":{"no_po":null,"date_po":null,"supplier":null,"shipping_to":null,"total_price_head":null,"vat":null,"net_price":null,"id_po":null,"file_ext":"png","file_type":"image\/png","file_size":633237,"file_name":"2015-02-23-Negative-World-left-hand.png"},"messages":[],"pageSize":null,"start":null,"total":null,"session":true,"other":null,"processingTime":null}"
sourceClass: "Ext.JSON"
sourceMethod: "decode"

这是我的代码:

Purchase.php

public function savePurchaseOrder($header,$detail)
{
    header('Content-Type: text/html');
    try{

        if(!$this->_user_id){
            $this->result->messages[] = 'Session Expired, Please re-login';
            return;
        }

        $_header = Zend_Json::decode($header, Zend_Json::TYPE_ARRAY);
        $_detail = Zend_Json::decode($detail, Zend_Json::TYPE_ARRAY);

        $userLogin = $this->_user_id;

        $table_header = new Zend_Db_Table('vending_tr_purchase_product_order_id');
        $table_detail = new Zend_Db_Table('vending_tr_purchase_product_order');
        $table_detail_pr = new Zend_Db_Table('vending_tr_purchase_product');
        //print_r($_detail);

    if (isset($_FILES['file_path']))
    {
            $errors = array();
            // define file attribute
            $file_name = $_FILES['file_path']['name'];
            $file_size = $_FILES['file_path']['size'];
            $file_tmp  = $_FILES['file_path']['tmp_name'];
            $file_type = $_FILES['file_path']['type'];
            $file_ext  = strtolower(end(explode('.', $_FILES['file_path']['name'])));

        $_newdata = array(
            'no_po' => $_header['no_po'],
            'date_po' => $_header['date_po'],
            'supplier' => $_header['supplier'],
            'shipping_to' => $_header['shipping_to'],
            'total_price_head' => $_header['total_price_head'],
            'vat' => $_header['vat'],
            'net_price' => $_header['net_price'],
            'id_po' => $_header['id_po'],
            //'file_path' => $_header['file_path'],
            'file_ext' => $file_ext,
            'file_type' => $file_type,
            'file_size' => $file_size,
            'file_name' => $file_name

        );

        $ext = array(
                "bmp",
                "jpeg",
                "jpg",
                "png",
                "gif",
                "xls",
                "xlsx",
                "doc",
                "docx",
                "pdf",
                "ppt",
                "pptx",
                "zip",
                "rar",
                "txt",
                "rtf"
        );
            // check the extension type on upload process
            if (in_array($file_ext, $ext) === false)
            {
                $errors[] = 'File format "' . $file_ext . '" is not allowed';
                return $this->result->messages[] = $errors;
            }
            // check the file size is not more than 25 MB = 26214400 bytes, 1 MB = 1048576 bytes
            if ($file_size > 26214400)
            {
                $errors[] = 'Sorry, the allowed max size is 25 MB';
                return $this->result->messages[] = $errors;
            }

        if($_header['action'] == 'C'){
            $record = array_merge($_newdata, array(
                'row_created_by' => $userLogin,
                'row_created_datetime' => date("YmdHis", strtotime($this->_datetime))
            ));
            $table_header->insert($record);
            $header_id = $this->_db->lastInsertId();
            $this->result->success = true;
        }

        if($_header['action'] == 'U'){
            $where = array("id_po = " . $this->_db->quote($_header['id_po']));
            $record = array_merge($_newdata, array(
                'row_changed_by' => $userLogin,
                'row_changed_datetime' => date("YmdHis", strtotime($this->_datetime))
            ));
            $table_header->update($record,$where);
            $this->result->success = true;
        }

        foreach ($_detail as $key) {

            $this->_db->beginTransaction();
            //$key['data']['warehouse'] = $_header['warehouse'];

            $_insertDetail = array(
                'qty_order' => $key['data']['qty_order'], 
                'no_pr' => $key['data']['no_pr'], 
                'purchase_product_name' => $key['data']['purchase_product_name'],
                'package' => $key['data']['package'],
                'unit' => $key['data']['unit'],
                'qty_approve' => $key['data']['qty_approve'],
                'purchase_id_tr' => $key['data']['purchase_id_tr'],
                'qty_hasil' => $key['data']['qty_hasil'],
                'unit_price' => $key['data']['unit_price'],
                'total_price' => $key['data']['total_price'],
                'warehouse_name' => $key['data']['warehouse_name']
            );

            $_updateDetailPO = array(
                'qty_order' => $key['data']['qty_order'],
                'qty_hasil' => $key['data']['qty_hasil']
            );

            if($key['type'] == 'U'){
                $where = array("purchase_id_tr = " . $this->_db->quote($key['data']['purchase_id_tr']));
                $table_detail_pr->update($_updateDetailPO,$where);
                $this->result->success = true;
            }

            if(!empty($key['data']['id'])){
                $where = array("id = " . $this->_db->quote($key['data']['id']));
                $table_detail->update($_insertDetail,$where);
                $this->result->success = true;
            }else{
                $_insertDetail['id_po'] = $header_id;
                $table_detail->insert($_insertDetail);
                $this->_db->commit();
                $updateStock = $this->updatePR($key['data']);
                $this->result->success = true;
            }

            if($key['type'] == 'D'){
                $where = array("id = " . $this->_db->quote($key['data']['id']));
                $table_detail->delete($where);
                $this->_db->commit();
                $this->result->success = true;
            }


        }
        return $_newdata;

    }


    }
    catch(Exception $_e) {
        $this->result->messages[] = new Sasmita_Transaction_Message('E', $_e->getMessage(), __CLASS__ . '/' . __FUNCTION__, __LINE__); 
    }
}

public function updatePR($data)
{
    //var_dump($data);
    //exit;
    $_sStock = $this->_db->select('a vending_tr_purchase_product','*')
    ->where('a.purchase_id_tr = ?',$data['purchase_id_tr'])
    //->where('a.warehouse_id = ?',$data['warehouse'])
    ->fetch();

    $table = new Zend_Db_Table('vending_tr_purchase_product');

    if(is_array($_sStock)){
        $pengurangan = $data['qty_hasil']-$data['qty_order'];
        $_updateDetailPO_R = array(
                'qty_order' => $data['qty_order'],
                'qty_hasil' => $pengurangan
            );
        $where = array("purchase_id_tr = " . $this->_db->quote($data['purchase_id_tr']));
        $a = $table->update($_updateDetailPO_R,$where);
        $this->result->success = true;
        //print_r ($a);
        //print_r($a->__toString());

    }else{
        $this->_db->beginTransaction();
        $_insertArray = array(
                'purchase_product_id' => $data['product_id'],
                'brand' => $data['data']['brand'],
                'package' => $data['data']['package'],
                'purchase_product_name' => $data['data']['product_name'],
                'qty_request' => $data['qty_request'],
                'qty_approve' => $data['qty_approve'],
                'qty_order' => $data['qty_approve'],
                'qty_hasil' => $data['qty_approve'],
                'approvel' => $data['approvel'],
                'unit_price' => $data['unit_price'],
                'unit' => $data['unit']
            );
        $table->insert($_insertArray);
        $this->_db->commit();
        $this->result->success = true;
    }

    return $this->result->success;
}

InputPO.js

requires: [
    'Ext.form.Panel',
    'Ext.form.FieldContainer',
    'Ext.form.field.Date',
    'Ext.form.field.File',
    'Ext.form.field.Hidden',
    'Ext.grid.Panel',
    'Ext.toolbar.Toolbar',
    'Ext.button.Button',
    'Ext.toolbar.Separator',
    'Ext.selection.CheckboxModel',
    'Ext.form.field.Number',
    'Ext.grid.column.Number',
    'Ext.grid.column.Date',
    'Ext.grid.column.Boolean',
    'Ext.grid.View',
    'Ext.grid.plugin.RowEditing',
    'Ext.grid.feature.Summary'
],

fileUpload: true,
height: 473,
width: 633,
title: '',

initComponent: function() {
    var me = this;

    Ext.applyIf(me, {
        items: [
            {
                xtype: 'form',
                bodyPadding: 10,
                title: '',
                items: [
                    {
                        xtype: 'fieldcontainer',
                        fieldLabel: '',
                        layout: {
                            type: 'hbox',
                            align: 'stretch'
                        },
                        items: [
                            {
                                xtype: 'container',
                                items: [
                                    {
                                        xtype: 'textfield',
                                        width: 250,
                                        fieldLabel: 'No. PO',
                                        labelWidth: 70,
                                        name: 'no_po'
                                    },
                                    {
                                        xtype: 'textfield',
                                        hidden: true,
                                        width: 250,
                                        fieldLabel: 'ID. PO',
                                        labelWidth: 70,
                                        name: 'id_po'
                                    },
                                    {
                                        xtype: 'datefield',
                                        width: 250,
                                        fieldLabel: 'Date',
                                        labelWidth: 70,
                                        name: 'date_po'
                                    },
                                    {
                                        xtype: 'textfield',
                                        width: 250,
                                        fieldLabel: 'Supplier',
                                        labelWidth: 70,
                                        name: 'supplier'
                                    }
                                ]
                            },
                            {
                                xtype: 'container',
                                margin: '0 0 0 10',
                                items: [
                                    {
                                        xtype: 'textfield',
                                        width: 250,
                                        fieldLabel: 'Total Price',
                                        labelWidth: 70,
                                        name: 'total_price_head'
                                    },
                                    {
                                        xtype: 'textfield',
                                        width: 250,
                                        fieldLabel: 'VAT',
                                        labelWidth: 70,
                                        name: 'vat'
                                    },
                                    {
                                        xtype: 'textfield',
                                        width: 250,
                                        fieldLabel: 'Net Price',
                                        labelWidth: 70,
                                        name: 'net_price'
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        xtype: 'container',
                        items: [
                            {
                                xtype: 'textfield',
                                hidden: true,
                                width: 511,
                                fieldLabel: 'Shipping To',
                                labelWidth: 70,
                                name: 'shipping_to'
                            },
                            {
                                xtype: 'filefield',
                                width: 511,
                                fieldLabel: 'Attachment',
                                labelWidth: 70,
                                name: 'file_path'
                            }
                        ]
                    },
                    {
                        xtype: 'hiddenfield',
                        name: 'action'
                    }
                ]
            },
            {
                xtype: 'gridpanel',
                height: 250,
                itemId: 'detailProduct',
                autoScroll: true,
                bodyBorder: false,
                title: 'Detail Product',
                store: 'vending.purchase.DetailProduct',
                dockedItems: [
                    {
                        xtype: 'toolbar',
                        dock: 'top',
                        items: [
                            {
                                xtype: 'button',
                                itemId: 'addDM',
                                iconCls: 'icon-add',
                                text: 'Add'
                            },
                            {
                                xtype: 'tbseparator'
                            },
                            {
                                xtype: 'button',
                                itemId: 'btnDelete_detailp',
                                iconCls: 'icon-delete',
                                text: 'Delete'
                            }
                        ]
                    }
                ],
                selModel: Ext.create('Ext.selection.CheckboxModel', {

                }),
                columns: [
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'purchase_id_tr',
                        text: 'Id'
                    },
                    {
                        xtype: 'gridcolumn',
                        hidden: true,
                        dataIndex: 'id_po',
                        text: 'Id'
                    },
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'warehouse_name',
                        text: 'Warehouse'
                    },
                    {
                        xtype: 'gridcolumn',
                        hidden: true,
                        dataIndex: 'qty_hasil',
                        text: 'Id'
                    },
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'no_pr',
                        text: 'No. PR'
                    },
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'purchase_product_name',
                        text: 'Product Name'
                    },
                    {
                        xtype: 'gridcolumn',
                        hidden: true,
                        dataIndex: 'string',
                        text: 'Brand'
                    },
                    {
                        xtype: 'gridcolumn',
                        hidden: true,
                        dataIndex: 'package',
                        text: 'Package'
                    },
                    {
                        xtype: 'gridcolumn',
                        hidden: true,
                        dataIndex: 'qty_approve',
                        text: 'Quantity Remaining'
                    },
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'unit',
                        text: 'Unit Order'
                    },
                    {
                        xtype: 'gridcolumn',
                        hidden: true,
                        dataIndex: 'qty_hasil',
                        text: 'Quantity Remaining'
                    },
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'qty_order',
                        text: 'Quantity',
                        editor: {
                            xtype: 'numberfield'
                        }
                    },
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'unit_price',
                        text: 'Unit Price',
                        editor: {
                            xtype: 'numberfield',
                            name: 'unit_price'
                        }
                    },
                    {
                        xtype: 'gridcolumn',
                        summaryType: 'sum',
                        dataIndex: 'total_price',
                        text: 'Total Price',
                        editor: {
                            xtype: 'numberfield',
                            name: 'total_price'
                        }
                    },
                    {
                        xtype: 'numbercolumn',
                        hidden: true,
                        dataIndex: 'number',
                        text: ''
                    },
                    {
                        xtype: 'datecolumn',
                        hidden: true,
                        dataIndex: 'date',
                        text: 'Date'
                    },
                    {
                        xtype: 'booleancolumn',
                        hidden: true,
                        dataIndex: 'bool',
                        text: 'Boolean'
                    }
                ],
                plugins: [
                    Ext.create('Ext.grid.plugin.RowEditing', {
                        clicksToEdit: 1
                    })
                ],
                features: [
                    {
                        ftype: 'summary'
                    }
                ]
            },
            {
                xtype: 'gridpanel',
                hidden: true,
                title: 'Lokasi Pengiriman',
                columns: [
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'string',
                        text: 'Nama gudang'
                    },
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'string',
                        text: 'Alamat'
                    },
                    {
                        xtype: 'gridcolumn',
                        dataIndex: 'string',
                        text: 'Shipping To'
                    },
                    {
                        xtype: 'numbercolumn',
                        hidden: true,
                        dataIndex: 'number',
                        text: 'Number'
                    },
                    {
                        xtype: 'datecolumn',
                        hidden: true,
                        dataIndex: 'date',
                        text: 'Date'
                    },
                    {
                        xtype: 'booleancolumn',
                        hidden: true,
                        dataIndex: 'bool',
                        text: 'Boolean'
                    }
                ]
            }
        ]
    });

    me.callParent(arguments);
}

});

这是我的控制器js动作点击按钮保存:

onButtonSave: function(button, e, eOpts) {

console.log("suksesss");
var me = this;
var _panel = me.getMainPanel();
var _tabpanel = _panel.down("#tabmaintain");
var _activetab = _tabpanel.getActiveTab();
var form = _activetab.down('form').getForm();
var values = form.getValues();
var myMask = new Ext.LoadMask(_activetab, {msg:"Please wait..."});


if(form.isValid())
{
    myMask.show();
    form.submit({
        url: '/jsonresult/Sasmita_Vending_Purchase/saveGoodsReceived',
        success: function(fp, o) {
            myMask.hide();
            //me.fnReloadTabAttachment();
            _activetab.close();
        },
        failure: function(fp,o){
            myMask.hide();
            if(!o.result.session)
            {
                Ext.Msg.alert('Error', o.result.messages);
            }
            else
            {
                if (o.result.messages[0].text)
                {
                    Ext.Msg.alert('Error', o.result.messages[0].text);
                }
                else
                {
                    Ext.Msg.alert('Error', o.result.messages);
                }
            }
        }
    });
}
},

这是foreach的错误。

1 个答案:

答案 0 :(得分:0)

  

您正在尝试解码无效的JSON字符串:

其次是:

  

警告:第331↵行的Purchase.php中为foreach()提供的参数无效   { “成功”:假的, “数据”:{ “no_po”:空, “date_po”:空, “供方” 空 “shipping_to”:空, “total_price_head”:空, “增值税”:空,“net_price “:空,” id_po “:空,” file_ext “:” PNG “ ”FILE_TYPE“: ”图像/ PNG“, ”FILE_SIZE“:633237, ”FILE_NAME“:” 2015年2月23日 - 负 - 世界 - 左-hand.png “},” 消息 “:[]” 的pageSize “:空,” 开始 “:空,” 总 “:空,” 会话 “:真,” 其它 “:NULL,” processingTime“:空} “

这可能意味着您的代码希望在警告之后解析JSON,而是同时获取两者。这会导致组合无效的JSON字符串。

解决方案是在触发之前检查您foreach的集合是否是集合(例如数组):

if(is_array($_detail)) {
    foreach ($_detail as $key) {
        // ...
    }
} else {
    // Do something else
}