CodeIgniter From Post Data Not Going through

2019-04-06 13:45发布

问题:

Im trying to make a file upload in CodeIgniter, how ever when I add enctype="multipart form-data" no post data will go through. At all not even the other fields. However when i dont add it, i can get the other post data, but of course no file upload. Whats going wrong here. Here is my view and controller:

View:

<h2>Add a New Album</h2>
<form enctype="multipart/form-data"  method="post" action="<?php echo base_url();?>index.php/photo/newAlbum">

                            <table style="margin-left:5px;">
                                <tr>
                                    <td>     Album Name:</td>
                                    <td><input type="text" name="name" /></td>
                                </tr>
                                <tr>
                                    <td>     Photo .zip File:</td>
                                    <td><input type="file" name="userfile" size="20" /></td>
                                </tr>
                                <tr>
                                    <td></td>
                                    <td><input type="submit" value="Upload Photo File" /></td>
                                </tr>
                            </table>

</form>

controller only contains:

var_dump($_POST);

Result is:

array(0) { }

回答1:

You can add this form attribute:
enctype="multipart/form-data;charset=utf-8"
Instead of:
enctype="multipart/form-data"
You can see this link.



回答2:

Actually, the multipart data like image/zip or some other blob data will be included in $_FILES array, not $_POST array.
I recommend you to use the upload library.

view:upload_form.php

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<?php echo form_open_multipart('upload/do_upload');?>

<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>

view:upload_success.php

<html>
<head>
<title>Upload Form</title>
</head>
<body>

<h3>Your file was successfully uploaded!</h3>

<ul>
<?php foreach($upload_data as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>

<p><?php echo anchor('upload', 'Upload Another File!'); ?></p>

</body>
</html>

controller:upload.php

<?php

class Upload extends CI_Controller {

 function __construct()
 {
  parent::__construct();
  $this->load->helper(array('form', 'url'));
 }

 function index()
 { 
  $this->load->view('upload_form', array('error' => ' ' ));
 }

 function do_upload()
 {
  $config['upload_path'] = './uploads/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '100';
  $config['max_width']  = '1024';
  $config['max_height']  = '768';

  $this->load->library('upload', $config);

  if ( ! $this->upload->do_upload())
  {
   $error = array('error' => $this->upload->display_errors());

   $this->load->view('upload_form', $error);
  } 
  else
  {
   $data = array('upload_data' => $this->upload->data());

   $this->load->view('upload_success', $data);
  }
 } 
}
?>

And that's all



回答3:

You need fix your do_upload, like this..

$this->upload->do_upload('name-of-input-file-element-of-your-form')

For example, in your view code, you have:

<input type="file" name="userfile" size="20" />

So, your do_upload line, should be like this:

$this->upload->do_upload('userfile')

Saludos!



回答4:

Interesting. The code you posted should work. I made a quick test script using your form, and both the $_FILES and $_POST data come through fine.

I found this previous question on SO: PHP--parsing multipart form data

It sounds like the same problem you're having. Unfortunately no real answer was reached for that problem. I would think this might be a server configuration issue - can you try the same code on another server and see if it functions?

The only other bit of information I can find was in the answer to this question, $_POST data returns empty when headers are > POST_MAX_SIZE

If you are trying to upload too large a file, apparently that can cause PHP to throw away the $_POST data as well. But if you've tried submitting your form without uploading a file (but still using "multipart/form-data"), and still don't see any $_POST data coming through, that doesn't apply.



回答5:

I just had the same problem. I solved it this way. in application/config/config.php you set the base_url please be aware of, that this url have to be exact the same as the one you're calling your web app. For me, I called my domain with www.sdfsd.com, but in the config file I wrote http://sdfsd.com and so the POST of the form didn't work. So I changed this and now everything is working!!

cheers



回答6:

Check for possible wrong redirection in CI layer. It could be caused by a wrong URL request (for example, a slash '/' missing at the end of the URL). Redirections delete any POST data (I was dealing with this).

So, be sure that you are using full URLs in form action.

Check for your .htaccess and $config['uri_protocol'] consistency too.



回答7:

I had this problem too. I don't agree with the above answers. I think the crucial thing is at what stage in your controller you attempt to get the post data. Perhaps you could post the relevant part of your controller. I found that both the post data and the upload data only become available after you call

this->upload->do_upload()

Obviously, you use an if statement to check for success. If successful,

$this->upload->data() 

returns the standard array with info about the uploaded file and

$this->input->post('name_of_your_form_element')

is populated with what you entered in the relevant form element.



回答8:

I had a similar problem - Turns out POST data is empty when you exceed max upload size / max post size. This answer fixed my problem and allowed me to see $_POST and $_FORM data: $_POST data returns empty when headers are > POST_MAX_SIZE