2012-01-16 2 views
1

Googleでこれを探し、stackoverflowでさまざまな回答をしました。そして、確率的に彼らには良い答えがありますが、私は自分のコードでそれをどうやって暗に与えることができないのですか?Codeigniterアップロードオプション

私は画像をアップロードするために私自身の公開機能を持っていましたが、今はオプションであることを望みます。この瞬間、検証をパスするには誰かがファイルをアップロードする必要がありますが、どうしたらいいですか?

私の機能:事前に

public function _do_upload_image() 
{ 
    $config['upload_path'] = './company_images/'; 
    $config['allowed_types'] = 'jpg|jpeg|png'; 

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

    if (!$this->upload->do_upload()) 
    { 
     $this->form_validation->set_message('_do_upload_image', $this->upload->display_errors()); 
    } 
    else 
    { 
     $this->_upload_data = $this->upload->data(); 
    } 

} 

おかげで、他の人のために

--edit--

は、答えが働いたと全くイメージがなかったとき、私は私のfile_nameのに他の名前を与えましたアップロードされました。それは次のようになります。

public function _do_upload_image() 
{ 
    $config['upload_path'] = './company_images/'; 
    $config['allowed_types'] = 'jpg|jpeg|png'; 
    $returnArr = array(); 

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

    if (!$this->upload->do_upload()) 
    { 
     $returnArr = array('file_name' => 'leeg.jpg'); 
    } 
    else 
    { 
     $returnArr = $this->upload->data(); 
    } 

    $this->_upload_data = $returnArr; 
} 

答えて

1

あなたがあなたのアップロード機能をオプションにする必要がある意味なら、あなただけ行うことができます。

 

public function _do_upload_image() 
{ 
    $config['upload_path'] = './company_images/'; 
    $config['allowed_types'] = 'jpg|jpeg|png'; 
    $returnArr = array(); 

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

    if ($this->upload->do_upload()) 
    { 
     $returnArr = $this->upload->data(); 
    } 
    return $returnArr; //just return the array, empty if no upload, file data if uploaded 
} 
 

希望を意味

+0

これはうまくいきますが、画像がアップロードされていなければ、私は['file_name']に値を与えることができます:leeg.jpg? – Augus

+0

Uhm nvm、私は問題を私が望むすべての機能で修正しました。私のポストを更新しないでください! – Augus

0

ファイルCodeIgniterには、必要に応じてアップロードしますその...完璧に..... .....

----------コントローラ---------

function file() 
{ 
$this->load->view('includes/template', $data); 
} 

function valid_file() 
{ 
$this->form_validation->set_rules('userfile', 'File', 'trim|xss_clean'); 

if ($this->form_validation->run()==FALSE) 
{ 
    $this->file(); 
} 
else 
{ 
    $config['upload_path'] = './documents/'; 
    $config['allowed_types'] = 'gif|jpg|png|docx|doc|txt|rtf'; 
    $config['max_size']  = '1000'; 
    $config['max_width']  = '1024'; 
    $config['max_height'] = '768'; 

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

    if (!$this->upload->do_upload('userfile',FALSE)) 
    { 
    $this->form_validation->set_message('checkdoc', $data['error'] = $this->upload->display_errors()); 

    if($_FILES['userfile']['error'] != 4) 
    { 
     return false; 
    } 

    } 
    else 
    { 
    return true; 
    } 
} 

私はちょうど$_FILES['userfile']['error'] != 4を使用することによってあなたはuはそれがunneccessoryすることができます。このオプションでそれを作るライン、

if($_FILES['userfile']['error'] != 4) 
{ 
return false; 
} 

$_FILES['userfile']['error'] != 4 is for file required to upload. 

を使用し、それが必要なファイルのために、このエラーを通過し、 は、他の種類の素晴らしい作品が偽の場合は、 がうまくいきたいと思っています。

関連する問題