0

嗨,这是一个简单的问题,但是我现在已经盯着它看了足够长的时间,意识到我根本没有看到错误。如果有人能看到哪里出了问题,我将非常感激。

public function create()
{
$this->load->model('ticket_model');

if($_POST)
    {
    // validate form
    if($this->_validate())
        {
        // save updates                                 
        foreach($_POST as $key => $value){if(!is_array($value)) $_POST[$key] = htmlspecialchars($value);}

        if ($_POST['subject'] == '')    $body_data['error'][] = "You did not enter a subject.";
        if ($_POST['priority'] == '')   $body_data['error'][] = "You did not select a priority.";
        if ($_POST['status'] == '')     $body_data['error'][] = "You did not select a status.";
        if ($_POST['ipAddress'] == '')  $body_data['error'][] = "You did not enter a ipAddress.";
        if ($_POST['text_area'] == '') $body_data['error'][] = "You did not enter a message.";
        else
            {
            if (filter_var($_POST['ipAddress'], FILTER_VALIDATE_IP,  FILTER_FLAG_IPV4) == FALSE) $body_data['error'][] = "IP Address is not valid IPV4 Address.";
            if (filter_var($_POST['ipAddress'], FILTER_VALIDATE_IP,  FILTER_FLAG_NO_PRIV_RANGE) == FALSE) $body_data['error'][] = "IP Address cannot be from RFC1918 private space.";
            if (filter_var($_POST['ipAddress'], FILTER_VALIDATE_IP,  FILTER_FLAG_NO_RES_RANGE) == FALSE) $body_data['error'][] = "IP Address cannot be from reserved range.";
            }
        if ($_FILES['filename']['name'] != '')
            {
            if ($_FILES['filename']['size'] > '1024000')
                {
                $body_data['error'][]                       = "The file you uploaded is too large.";

                unlink($_FILES['filename']['tmp_name']);

                $body_data['ticket_list']       = $this->ticket_model->list_ticket();
                $body_data['ticket_details']    = $this->ticket_model->get_ticket($ticket_id);
                $body_data['ticket_summary']    = $this->ticket_model->list_ticket_summary($ticket_id);

                $body_data['precan_list']       = $this->ticket_model->list_messages();
                $body_data['users_list']        = $this->ticket_model->list_users();
                $foot_data['accordian_active'] = 5;

                $this->load->view('head',$head_data);
                $this->load->view('sidebar/service',$head_data);
                $this->load->view('ticket/edit',$body_data);
                $this->load->view('foot',$foot_data);
                return;
                }
            else
                {
                //the file is under the specified size. so copy it from temp to import folder and proccess
                $thisFileHumanName      = $_FILES['filename']['name'];
                $thisFileSize           = $_FILES['filename']['size'];
                $thisServerFileName     = strtoupper(uniqid('A'));
                $thisFileType           = $_FILES['filename']['type'];

                $temp_file_location = $this->config->item('rootpath').'/assets/ticketuploads/'.$thisServerFileName;

                if (!move_uploaded_file($_FILES['filename']['tmp_name'], $temp_file_location))
                    {
                    $body_data['error'][]                       = "File could not be moved due to a permissions error.";

                    unlink($_FILES['filename']['tmp_name']);

                    $body_data['ticket_list']       = $this->ticket_model->list_ticket();
                    $body_data['ticket_details']    = $this->ticket_model->get_ticket($ticket_id);
                    $body_data['ticket_summary']    = $this->ticket_model->list_ticket_summary($ticket_id);

                    $body_data['precan_list']       = $this->ticket_model->list_messages();
                    $body_data['users_list']        = $this->ticket_model->list_users();
                    $foot_data['accordian_active'] = 5;

                    $this->load->view('head',$head_data);
                    $this->load->view('sidebar/service',$head_data);
                    $this->load->view('ticket/edit',$body_data);
                    $this->load->view('foot',$foot_data);
                    return;
                    }
                }
            }

        //clean error array
        $body_data['error']                     = array_filter($body_data['error']);

        if ($body_data['error'])
            {
            $body_data['ticket_list']       = $this->ticket_model->list_ticket();
            $body_data['ticket_details']    = $this->ticket_model->get_ticket($ticket_id);
            $body_data['ticket_summary']    = $this->ticket_model->list_ticket_summary($ticket_id);

            $body_data['precan_list']       = $this->ticket_model->list_messages();
            $body_data['users_list']        = $this->ticket_model->list_users();

            unlink($_FILES['filename']['tmp_name']);

            $foot_data['accordian_active'] = 5;

            $this->load->view('head',$head_data);
            $this->load->view('sidebar/service',$head_data);
            $this->load->view('ticket/edit',$body_data);
            $this->load->view('foot',$foot_data);
            return;
            }
        else
            {   

            $_POST['userId'] = $this->session->get_user_id();

            $thisMessageId = $this->ticket_model->save_message($_POST);

            if ($_FILES['filename']['name'] != '')
                {
                //set variables for save
                $_POST['file_path'] = $temp_file_location;
                $_POST['file_name'] = $thisFileHumanName;

                $_POST['file_size'] = $thisFileSize;
                $_POST['file_type'] = $thisFileType;
                $_POST['messageId'] = $thisMessageId;

                $this->ticket_model->save_upload($_POST);
                }
            $this->ticket_model->save_ticket($_POST);

            redirect(base_url().'ticket/');
            return;
            }
        }
    }
$body_data['ticket_list']        = $this->ticket_model->list_ticket();
$body_data['message_list']       = $this->ticket_model->list_message($ticket_id);
$body_data['customer_list']      = $this->ticket_model->list_customers();
$body_data['users_list']         = $this->ticket_model->list_users();

$foot_data['accordian_active'] = 5;
$foot_data['contact_search']   = true;
$this->load->view('head',$head_data);
$this->load->view('sidebar/service',$head_data);
$this->load->view('ticket/create',$body_data);
$this->load->view('foot',$foot_data);
return;
}

这是我的代码,一切进展顺利,除了我保存上传的部分,因为似乎没有任何东西在触发模型,即使从提交中发布了一个文件并且那里发布的文件名是!= ''……例如

if ($_FILES['filename']['name'] != '')
{
    //set variables for save
    $_POST['file_path'] = $temp_file_location;
    $_POST['file_name'] = $thisFileHumanName;

    $_POST['file_size'] = $thisFileSize;
    $_POST['file_type'] = $thisFileType;
    $_POST['messageId'] = $thisMessageId;

    $this->ticket_model->save_upload($_POST);
}

如果这是愚蠢的错误,我深表歉意。

4

2 回答 2

2

你为什么这样做?Codeigniter 有一个用于上传文件的内置类。您还应该使用输入类而不是$_POST. 它会让事情变得容易很多!

至于你的代码。您实际上是在设置 $_POST 变量并尝试在save_ticket. 你不能那样做。

预定义的 $_POST 变量用于从使用 method="post" 发送的表单中收集值

您正试图以相反的方式使用它。

因此,要使其工作,将 $_POST 更改为 $something 并且它应该可以工作,但它仍然不是要走的路。

//set variables for save
$something['file_path'] = $temp_file_location;
$something['file_name'] = $thisFileHumanName;

$something['file_size'] = $thisFileSize;
$something['file_type'] = $thisFileType;
$something['messageId'] = $thisMessageId;

$this->ticket_model->save_upload($something);
于 2012-07-12T15:32:52.320 回答
0

没有这一套…………enctype="multipart/form-data"

脸红到此为止。

于 2012-07-13T16:00:07.070 回答