Welcome Guest, Not a member yet? Register   Sign In
Bug Upload- myfiles are not working
#1

[eluser]Unknown[/eluser]
Hi,
i'm a CI biginner and i'm working in a aplication with uploading files. It's a simple controller. Here's the code:
Code:
<?php
class Designers extends CI_Controller
{
    private $filename = array(1=>'',2=>'');
    private $debug = true;
    
    public function __construct()
    {
parent::__construct();
$this->load->model("designers_model", "designers");
    }

    // criar = create
    public function criar()
    {
     $dados = $this->input->post();
            
        $this->load->view("admin/inc/header");
     $this->load->view("admin/designers/criar");

     // Caso a variavel $dados estiver vazia, não é feita a inserção
     if($dados != array())
     {
            $this->uploadFiles($dados["nome_designer"]);

            $dados["thumb"] = $this->filename[1];
         $dados["fotoperfil"] = $this->filename[2];
                
            $this->designers->insert($dados);
         redirect("admin/designers/criar", "refresh");
     }
    }

    // alterar = edit
    public function alterar()
    {
     /* resgata código do usuário pela URL */
     $url = $this->uri->segment_array();
     $id = $url[4];
        
        // dados = data
     $dados = $this->designers->getDesignerByIdAdmin($id);
        $this->filenames[1] = $dados["thumb"];
        $this->filenames[2] = $dados["fotoperfil"];

     if($this->input->post())
     {    
            $this->uploadFiles($dados["nome"]);
            $post = $this->input->post();
        
            /* Verifica se alguma imagem foi alterada
             * e se foi atualiza-la no array $post
             */
            
            $post["thumb"] = $this->filename[1];
            $post["fotoperfil"] = $this->filename[2];

         //atualiza o array $dados com as informações do banco já atualizadas
         $dados = $this->designers->getDesignerByIdAdmin($id);

            //$this->uploadFiles($dados["nome"]);
            $this->designers->update($post, $id);
     }

        $this->load->view("admin/inc/header");
$this->load->view("admin/designers/alterar", $dados);
    }


    protected function uploadFiles($nome = "")
    {
        try {
            $config['upload_path'] = 'img/designers/thumb/';
            $config['allowed_types'] = 'jpg|jpeg|png|gif';
            $config['max_size'] = '5000';

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


            //Adicionar nomes dos campos aqui abaixo, se tiver que adicionar mais campos:
            $campos = array(
                1 => 'thumb',
                2 => 'fotoperfil'
            );

            $files = count($campos);

            for ($i = 1; $i <= $files; $i++) {
                if (isset($_FILES[$campos[$i]])) {
                    if ($_FILES[$campos[$i]]['size'] != 0) {
                        
                        $filenameArray = explode('.', $_FILES[$campos[$i]]['name']);
                        $fileExtension = '.' . $filenameArray[count($filenameArray)-1];
                        //$prefix = 'AC_'.$_POST['codigo'];
                        
                        if ($i != 1)
                            $config['upload_path'] = 'img/designers/foto/';
                        
                        while (true) {
                            $config['file_name'] = $this->filename[$i] = uniqid($nome).".".$fileExtension;
                            if (!file_exists($config['upload_path'] . $config['file_name'])) break;
                        }
                        
                        $this->upload->initialize($config);
                        
                        if (!$this->upload->do_upload($campos[$i])) {
                            die($campos[$i] . ": " . $this->upload->display_errors());
                        }
                        //print_r($updata = $this->upload->data());
                    }
                }
            }
        } catch (Exception $e) {
            echo $e->getMessage();
        }
    }
}

The criar(create) function is running ok, my bug are at alterar(edit) function. For some reason the $_FILES array are bringing a empty array.

Can you help me?
Thanks a lot.


Sorry for the code in portuguese, but it was asked by the client.
Thanks


[email protected]
#2

[eluser]Chathuranga Tennakoon[/eluser]
can you post the coding of your view also? then i can go through the whole coding and figure out what actually happens..




Theme © iAndrew 2016 - Forum software by © MyBB