Welcome Guest, Not a member yet? Register   Sign In
Simple upload problem
#1

[eluser]vile[/eluser]
can anyone help me solve this simple problem.. please.

i trying to upload a photo but it wont work. is it possible to upload photo while posting other data? any help is appreciated thanks Smile


heres my view code:

Code:
<?php
$formattrib = array('enctype' => 'multipart/form-data')
?>

<?=form_open_multipart('/admin/admin/add_agents',$formattrib);?>
<table align="center" width="600" border="0" cellpadding="0" cellspacing="0">
<tr>
    <td>Firstname:</td>
    <td>
    &lt;?php
        $data = array(
              'name'        => 'firstname',
              'value'       => $this->validation->firstname,
              'maxlength'   => '25',
              'size'        => '30'
            );
        echo form_input('firstname');
    ?&gt;
    </td>
</tr>

<tr>
    <td>Lastname:</td>
    <td>
        &lt;?php
        $data = array(
              'name'        => 'lastname',
              'value'       => $this->validation->lastname,
              'maxlength'   => '25',
              'size'        => '30'
            );
        echo form_input('lastname');
    ?&gt;
    </td>
</tr>

<tr>
    <td>Chat Nick:</td>
    <td>
        &lt;?php
        $data = array(
              'name'        => 'nick',
              'value'       => $this->validation->nick,
              'maxlength'   => '25',
              'size'        => '30'
            );
        echo form_input('nick');
    ?&gt;
    </td>
</tr>


<tr>
    <td>Photo:</td>
    <td>&lt;input type="file" name="userphoto" size="38" /&gt;&lt;/td>
</tr>
<tr>
    <td colspan="2" align="Center"><br />&lt;?=form_submit('submit', ' Create New User ');?&gt;</td>
</tr>
</table>
&lt;?=form_close();?&gt;

Controller code:
Code:
function add_agents()
    {
        $rules['firstname']    = "required";
        $rules['lastname']    = "required";
        $rules['nick']    = "required";
        $this->validation->set_rules($rules);
        $fields['firstname'] = 'First Name';
        $fields['lastname'] = 'Last Name';
        $fields['nick'] = 'Nickname';
        
        $this->validation->set_fields($fields);
        $data['username'] = false;
        $data['msg'] = false;
        
        if ($this->validation->run() == FALSE) {
            $this->load->view("admin/add_agents_view", $data);
        } else {
            print_r($_POST);
            
            
            $firstname = $_POST['firstname'];
            $lastname = $_POST['lastname'];
            $nick = $_POST['nick'];
            $userphoto =  $_POST['userphoto'];

            $data = $this->admin_model->add_agent($firstname, $lastname, $nick, $userphoto);
            if ($data['username']) {
                $data['msg'] = 'Username <b>'.$data['username'].'</b> with password <b>'.$data['username'].'</b> added successfully!';
                $this->load->view("admin/add_agents_view", $data);
            } else {
                $this->load->view("admin/add_agents_view", $data);
            }
        }
    }

and heres my model code:

Code:
function add_agent($firstname, $lastname, $nick, $userphoto) {
            
            $username = $firstname.'.'.$lastname;
            
            $this->db->select('agent_id');
            $this->db->where('username', $username);
            $query = $this->db->get('agent');
            if ($query->num_rows() > 0) {
                $data['msg'] = 'This user already exists';
                $data['username'] = false;
            }  else {
                $data['msg'] = '';
                $data['username'] = $username;
                
                $params = array(
                   'fname' => $firstname ,
                   'lname' => $lastname ,
                   'username' => $username,
                   'pass' => $username,
                   'nick' => $nick,
                   'active' => 1
                );
                $this->db->insert('agent', $params);
                $directory = "/home/chat/codeigniter/chat/agents/".$username;
                
                $directory1 = $directory.'/client_xml/';
                $directory2 = $directory.'/archive/';
                //$exec = 'mkdir '.$directory;
                mkdir($directory1, 0777, true);
                mkdir($directory2, 0777);

            
            $config['upload_path'] = '/home/chat/codeigniter/chat/agents/'.$username;
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size']    = '100';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            
        $this->load->library('upload', $config);
        $this->upload->do_upload($userphoto);        
            }
            
            return $data;
        }
#2

[eluser]vile[/eluser]
i tried to display $_POST variables using print_r() but i cant see any data from the file form.
i'm guessing, maybe its not possible to upload an file and post some data at the same time. if that's the case. i will separate uploading and posting data. i just need to confirm if i'm getting the right idea.
#3

[eluser]Weblizard[/eluser]
I did not check your code but I know that is possible to post data within upload form
#4

[eluser]vile[/eluser]
maybe there is something wrong in my code i will try again thanks
#5

[eluser]mglinski[/eluser]
Perhaps you are not passing the data array to the form_ functions.
You might want to check the documentation again on the exact functions you are using.
If you are still drawing a blank let me know. I know why you are having problems, but it is best if you figure it out on your own.
-Matt
#6

[eluser]vile[/eluser]
[quote author="XtraFile" date="1221753282"]Perhaps you are not passing the data array to the form_ functions.
You might want to check the documentation again on the exact functions you are using.
If you are still drawing a blank let me know. I know why you are having problems, but it is best if you figure it out on your own.
-Matt[/quote]


ok. Now i know why i cant see anything when i display array contents.. $_FILES this is the firstime i use this function. Big Grin . anyway this simple script wont work grrrrr.. can u please tell me whats wrong in my code. thanks

this is my model:
Code:
function add_agent($FILE) {
$directory = "/home/vile";
$config['upload_path'] = $directory;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']    = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';
$this->load->library('upload', $config);
$this->upload->do_upload($FILE['userfile']);
}


this is my controller:
Code:
function uploadpic()
    {
        $data = $this->admin_model->add_agent($_FILES);
    }


view code:
Code:
&lt;?=form_open_multipart('/admin/uploadpic');?&gt;
Photo:&lt;input type="file" name="userfile" size="38" /&gt;
&lt;?=form_submit('submit', ' Create New User ');?&gt;

&lt;?=form_close();?&gt;


ERROR:
Code:
A PHP Error was encountered

Severity: Warning

Message: Illegal offset type in isset or empty

Filename: libraries/Upload.php

Line Number: 138
#7

[eluser]Colin Williams[/eluser]
Code:
$this->upload->do_upload($FILE['userfile']);

This is erroneous (on two levels, actually, or just one if you've renamed $_FILES to $FILE). You just need to pass the name of the field (or if it's 'userfile' you can send no argument)

Code:
$this->upload->do_upload('file_field_name');
$this->upload->do_upload(); // Assumes to use $_FILES['userfile']
#8

[eluser]vile[/eluser]
i still dont understand this. :down:
[quote author="Colin Williams" date="1221823911"]
Code:
$this->upload->do_upload('file_field_name');
$this->upload->do_upload(); // Assumes to use $_FILES['userfile']
[/quote]


i change my code to this. but it didnt work:
model:
Code:
function add_agent($_FILES) {
$directory = "/home/vile";
$config['upload_path'] = $directory;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']    = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';
$this->load->library('upload', $config);
$this->upload->do_upload();
}

controller:
Code:
function uploadpic()
    {
        $data = $this->admin_model->add_agent($_FILES);
    }

view:
Code:
&lt;?=form_open_multipart('/admin/uploadpic');?&gt;
Photo:&lt;input type="file" name="userfile" size="38" /&gt;
&lt;?=form_submit('submit', ' Create New User ');?&gt;

&lt;?=form_close();?&gt;
#9

[eluser]vile[/eluser]
Holy cow! it worked!!! thanks a lot!! Big Grin
i wasnt supposed to pass $_FILES; lesson learned :gulp:
thanks a lot!!!!!
#10

[eluser]xwero[/eluser]
You don't have to add the $_FILES global to the method. It's a global so you can use it anywhere you want.

Is it possible you loaded the upload library somewhere else? If it is already loaded your config settings will not be passed on. To be on the safe side you can change your code to
Code:
$this->load->library('upload'); // load if it hasn't already been loaded
$this->upload->initialize($config); // pass the settings to the library




Theme © iAndrew 2016 - Forum software by © MyBB