Welcome Guest, Not a member yet? Register   Sign In
CI upload file demo not working?
#1

(This post was last modified: 03-19-2016, 06:21 AM by seaguest.)

I am using codeigniter 2.2.6 for my server (I am a newbie in CI), when I tried to make the upload file demo work, but nothing happened when I clicked on the submit button.

Here is the user guide that I followed:

http://www.codeigniter.com/userguide2/li...ading.html

the view file:

PHP Code:
    <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> 

the controller:

PHP Code:
    <?php
    
class Upload extends CI_Controller {
        function __construct() {
            parent::__construct ();
            $this->load->helper ( array (
                    'form',
                    'url' 
            ) );
        }
        function index() {
            echo CI_VERSION;
            
            $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 );
            
            echo 
"step 1";
            
            if 
(! $this->upload->do_upload ()) {
                echo "step 2";
                
                $error 
= array (
                        'error' => $this->upload->display_errors () 
                );
                
                $this
->load->view 'upload_form'$error );
            } else {
                echo "step 3";
                    
                $data 
= array (
                        'upload_data' => $this->upload->data () 
                );
                
                $this
->load->view 'upload_success'$data );
            }
        }
    }
    ?>

I can see the browse and submit button, after I select a file, the file name is correctly displayed but when I click on submit, nothing happens, it looks like that the submit click is not handled.

As you can see, I set 3 echo "step ." to follow the flow, but none of them get printed, I don't know why.

Could anyone help? please explain me the logic behind that button click handling.
Reply
#2

Add public in front of your function. Public function foo.
Keep calm.
Reply
#3

(03-19-2016, 06:23 AM)arma7x Wrote: Add public in front of your function. Public function foo.

thanks for your reply.
I added public, but still the same things, function do_upload is not called at all.
Reply
#4

I guess the following code has some pbs:

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

the function do_upload is not called at all, how should it be used?
Reply
#5

OK, I found the problem, apparently there is a problem of
<?php echo form_open_multipart('upload/do_upload');?>

It generate this wrong url:
<form enctype="multipart/form-data" accept-charset="utf-8" method="post" action="http://::1/upload/do_upload">
<input type="file" size="20" name="userfile">
<br>
<br>
<input type="submit" value="upload">
</form>

I changed the action="http://::1/upload/do_upload" to action="http://localhost/upload/do_upload" and it works.

Could anyone explain why
<?php echo form_open_multipart('upload/do_upload');?>
generates the wrong url?
Reply
#6

You probably left the $config['base_url'] setting blank (in application/config/config.php). Then, CI is best-guessing it. I think your hosts file (in Windows/System32/Drivers/Etc) there is no reference to localhost, but only to ::1.
The easiest way to solve the problem is assigning the right value to $config['base_url']. In your case:
PHP Code:
$config['base_url'] = 'http://localhost/'
Reply
#7

Indeed it is empty, because I have removed the index.php in the url path.
Reply
#8

(03-20-2016, 01:39 AM)seaguest Wrote: Indeed it is empty, because I have removed the index.php in the url path.

index.php in your URIs has nothing to do with the base_url setting.
Also, upgrade to the latest 3.0.x version, now - you wouldn't have this problem with it.

2.x is unsupported, there's no point in trying to teach yourself in it.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB