0

I Have HTML form with some text input and file input. I want to submit form using jquery form plugin and it is working ok, but the problem is when I select no file in file input, the file input field become part of $_POST with empty value I don't want it in $_POST array.

Please help.

This is My Code I am using CodeIgniter - @Let's Code

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'] = '';
}
2
  • Do you have isset in your code? Commented Jul 16, 2013 at 7:24
  • 2
    show some code We will be able to understand your problem better then Commented Jul 16, 2013 at 7:25

5 Answers 5

1

Any files uploaded will be in the $_FILES array. You can find all the information that you need in the php manual: http://www.php.net/manual/en/features.file-upload.post-method.php

Sign up to request clarification or add additional context in comments.

2 Comments

You right, But the problem is when I select no file it become part of $_POST array with empty value
See @Manish answer for a possible solution
1

You get $_POST values array and a quick way to remove empty elements from an array is using array_filter without a callback function. This will also remove 0s (zeroes) though.

$myArray = array_filter( $_POST );

Comments

0

Why do you need not to have this field in $_POST? You should just ignore it on the server if it is empty.

If there is any reason, you can try to remove the element from DOM before sending form. (http://api.jquery.com/remove/).

Comments

0

how about checking it before posting whether input file exist or not, if not then remove that tag and then post.

Comments

0

Hi I created one php file which uploads the image. It might be helpful to you. I am uploading an image and saving it into database image with its name.

here is 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>

here is 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. "; 

            }
            }
            }
            ?>

structure of store table of photo database is on this link:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.