• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File Uploading Class

Hi eveyone,

I’m new to CI so please don’t jump down my neck !

I’m working on migrating a php site to CI and all is going well - I love it.

But (there’s always a but), having followed the ‘File Uploading Class’ example from the user guide and used the code provided there, I cannot get my uploader to work. It keeps returning the error ‘You did not select a file to upload.’.

I have used $this->output->enable_profiler() in my controller and it shows no POST data.

In an effort to fix that (and on a suggestion from a different forum), I have tried setting up my form the HTML way: <form action=”/pk/upload/do_upload” method=“POST” ENCTYPE=“multipart/form-data”>. When I do that, the profiler displays ‘$_POST[‘userfile’]’ as the POST data, but the error still remains the same and nothing uploads.

I’ve Googled many many topics and can’t find anything that resolves the problem.

Help !

Files don't get uploaded in $_POST with PHP, they're in $_FILES.

One thing to check is to make sure you are using the NAME of your file field in the controller.
If the file field was:
<input type="file" name="userfile" size="20" />

Since the name is "userfile" you don't have to do anything here (it's CI's default name):

But if it's different, like "userupload", then you'd have to:

You need to post your code here for the file upload or we will not be able to help you.

Please wrap your code in code tags or use the POST REPLY for the full forum editor.

Thank you for helping.

I am using the example code taken from the user guide -

The Form - upload_form.php

<title>Upload Form</title>

<?php echo $error;?>

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

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

<br /><br />

&lt;input type="submit" value="upload" /&gt;



The Success View - upload_success.php

&lt;title&gt;Upload Form&lt;/title&gt;

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

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

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


The Controller - upload.php


class Upload extends CI_Controller {

function __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);
   $data = array('upload_data' => $this->upload->data());

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

I understand that the uploaded files don't normally show up in $POST, but another curiosity is that when I open a form using the form helper, ordinary text field POST data doesn't show up either - nothing gets posted. When I open the form using the HTML style, the text field data posts correctly, hence my experiment with that. I'm beginning to wonder could it be a configuration issue ....

You can go through your php.ini file and check things like allow uploads etc;

Also the max post size has an effect on uploads.

Try running the below in your do_upload method.
echo $this->input->post(NULL, TRUE); // returns all POST items with XSS filter

Thanks for the suggestion,

I have placed the suggested code immediately after

if ( ! $this->upload->do_upload())

and it produces a blank page .... no output, no errors, and exits as expected. So it seems to be getting into the do_upload method, but there's nothing to upload.

Looks like nothing is being posted. This leads me back to my issue on other forms (mentioned earlier) that nothing posts from non-file type inputs from forms that are opened with the form helper. Seems too similar to be co-incidental, but what is causing it ??

You're sure there are no errors in your error logs? Do you have display_errors enabled?

For one thing, $this->input->post(NULL, TRUE) is an ARRAY. You can't directly echo an array (use print_r()). You should be getting an error there. But, according to your form that you posted, there aren't any other POST fields in your form so it should be an empty array anyway. You only have a single file input, so check the $_FILES superglobal.

Try skipping the views for troubleshooting, like this:
if ( ! $this->upload->do_upload())
   echo 'FILES array:<br><pre>';
   echo '</pre><br>Upload Errors:<br>';
   echo $this->upload->display_errors();
   echo 'Upload Success:<br><pre>';
   echo '</pre>';

Logging is turned on - here's the log content for an upload attempt

DEBUG - 2014-09-24 20:56:44 --&gt; Config Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; Hooks Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; Utf8 Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; UTF-8 Support Enabled
DEBUG - 2014-09-24 20:56:44 --&gt; URI Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; Router Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; Output Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; Security Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; Input Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; Global POST and COOKIE data sanitized
DEBUG - 2014-09-24 20:56:44 --&gt; Language Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; Loader Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; Helper loaded: url_helper
DEBUG - 2014-09-24 20:56:44 --&gt; Helper loaded: html_helper
DEBUG - 2014-09-24 20:56:44 --&gt; Helper loaded: form_helper
DEBUG - 2014-09-24 20:56:44 --&gt; Database Driver Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; Upload Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; Controller Class Initialized
DEBUG - 2014-09-24 20:56:44 --&gt; Upload class already loaded. Second attempt ignored.
DEBUG - 2014-09-24 20:56:44 --&gt; Language file loaded: language/english/upload_lang.php
ERROR - 2014-09-24 20:56:44 --&gt; You did not select a file to upload.

And printing $_Files displays

Array ( )

Still looks like there is nothing being posted to $_Files, but why ?

Ok, what version of CI are you running?

You could try and download it again, I have got downloads of CI that seemed to work but then they did not work.

Download and install it again.

Also post your code for your form.

I'll download a new copy and try that.

The version I'm using is 2.2.0 - downloaded last week.

My form code is the test form from the user guide - exactly as posted above.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.