发布带有文件输入字段的html表单

时间:2013-07-16 07:23:25

标签: php jquery html ajax

我有 HTML 表单,其中包含一些文本输入和文件输入。我想使用 jquery 表单插件提交表单,它工作正常,但问题是当我在文件输入中没有选择文件时,文件输入字段成为$_POST的一部分,空值我不希望它出现在$_POST数组中。

请帮忙。

这是我的代码我正在使用CodeIgniter - @ Let代码

public function saveDataSourceAttributesValues() {
    $data_source_id = $this->input->post('data_source_id');
    $attributes_values_row_id = $this->input->post('row_id');
    $text_attributes = $this->input->post(); // Text Input fields
    $images_attributes = $_FILES; // Files Input field

    if ($attributes_values_row_id == '') {
        $last_row_id = $this->DataSourceModel->getDataSourceAttributesValuesLastRowId($data_source_id);
        if ($last_row_id) {
            $row_id = (int) $last_row_id + 1;
        } else {
            $row_id = 1;
        }
    } else {
        //Edit case
        $row_id = $attributes_values_row_id;
    }

    if (!empty($text_attributes)) {
        foreach ($text_attributes as $attribute_id => $value) {
            if (is_numeric($attribute_id)) {
                $temp_data = array(
                    'data_source_id' => $data_source_id,
                    'data_source_attribute_id' => $attribute_id,
                    'value' => $value,
                    'row_id' => $row_id,
                    'created' => date('Y-m-d H:i:s')
                );

                if ($attributes_values_row_id == '') {
                    $this->CommonModel->insert('data_source_attribute_value', $temp_data);
                } else {
                    $this->DataSourceModel->updateDataSourceAttributeValue($attribute_id, $row_id, $temp_data);
                }
            }
        }
    }

    if (!empty($images_attributes)) {

        $this->load->library('upload');
        $config['upload_path'] = './data_sources/images/';
        $config['allowed_types'] = '*';
        $config['max_size'] = '10000';
        //$config['overwrite'] = TRUE;
        //$config['remove_spaces'] = TRUE;

        foreach ($images_attributes as $attribute_id => $value) {
            if (is_numeric($attribute_id)) {
                $config['file_name'] = str_replace(' ', '_', $_FILES[$attribute_id]['name']);
                $image_path = $config['upload_path'] . '/' . $config['file_name'];
                $this->upload->initialize($config);
                if ($this->upload->do_upload($attribute_id)) {
                    $temp_data = array(
                        'data_source_id' => $data_source_id,
                        'data_source_attribute_id' => $attribute_id,
                        'value' => $image_path,
                        'row_id' => $row_id,
                        'created' => date('Y-m-d H:i:s')
                    );

                    if ($attributes_values_row_id == '') {
                        $this->CommonModel->insert('data_source_attribute_value', $temp_data);
                    } else {
                        $this->DataSourceModel->updateDataSourceAttributeValue($attribute_id, $row_id, $temp_data);
                    }
                }
            }
        }
    }

    $response['response'] = 200;
    $response['message'] = 'Data source attributes values has been saved successfully.';
    $response['error'] = '';
    $response['data'] = '';
}

5 个答案:

答案 0 :(得分:1)

上传的任何文件都在$ _FILES数组中。您可以在php手册中找到所需的所有信息:http://www.php.net/manual/en/features.file-upload.post-method.php

答案 1 :(得分:1)

您获得$_POST值数组,并且从数组中删除空元素的快速方法是使用array_filter而不使用回调函数。这也将删除 0s (零)。

$myArray = array_filter( $_POST );

答案 2 :(得分:0)

为什么你不需要在$ _POST中包含这个字段?如果它是空的,你应该在服务器上忽略它。

如果有任何原因,您可以尝试在发送表单之前从DOM中删除该元素。 (http://api.jquery.com/remove/)。

答案 3 :(得分:0)

如何在发布输入文件是否存在之前检查它,如果没有,则删除该标记然后发布。

答案 4 :(得分:0)

您好我创建了一个上传图片的php文件。它可能对你有所帮助。我正在上传图像并将其保存到数据库图像中,并带有其名称。

这里是index.php

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>

    <body>
    <form action="get.php" method="post" enctype="multipart/form-data">

    <p>
    <label for="image">Upload an image</label>
    <input type="file" name="image" id="image" />

    </p>
    <p>
      <input type="submit" name="submit" id="submit" value="Submit" />
    </p>
    </form>
    </body>
    </html>

这是get.php

        <?php

            //connect to database
            mysql_connect("localhost","root","") or die(mysql_error());
            mysql_select_db("photo") or die(mysql_error());

            //file properties


            $file = $_FILES['image'];//['tmp_name'];

            if (!isset($file))
            echo "Please select an image.";
            else
            {

            $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
            $image_name = addslashes($_FILES['image']['name']);
            $image_size = getimagesize($_FILES['image']['tmp_name']);

                if ($image_size==FALSE)
            echo "That's not an image.";
            else
            {
            if (!$insert = mysql_query("INSERT INTO store VALUES('','$image_name','$image')"))
            echo "Problem uploading image.";
            else
            {
            $lastid = mysql_insert_id();
            echo "image uploaded. "; 

            }
            }
            }
            ?>

照片数据库商店表的结构在this link

相关问题