2017-11-16 2 views
0

2つの異なるディレクトリに1つの画像を挿入しようとしていますが、最初のディレクトリに保存していますが2番目のディレクトリには保存していません。下記のコードをご確認いただき、お手伝いしてください。事前に感謝しています。codeigniterを使用して2つの異なるディレクトリに1画像を挿入する方法

public function image() 
{ 
    $data = array(); 
    $error = array(); 
    $config1=array(
     'upload_path'=>'upload/', 
     'allowed_types'=>'jpg|jpeg|png|bmp', 
     'max_size'=>0, 
     'filename'=>url_title($this->input->post('file')) 
    ); 

    $this->load->library('upload',$config1); 
    if($this->upload->do_upload('file')){ 
     $error = array('error' => $this->upload->display_errors()); 
     echo "<pre>"; 
     print_r($error); 
     exit(); 
    }else { 
     $fdata = $this->upload->data(); 
     $data['image'] = 'upload/' . $fdata['file_name']; 
    } 

    $config2=array(
     'upload_path'=>'upload/images/', 
     'allowed_types'=>'jpg|jpeg|png|bmp', 
     'max_size'=>0, 
     'filename'=>url_title($this->input->post('file')) 
    ); 
    $this->upload->initialize($config2); 

    if (!$this->upload->do_upload('file')){ 
     $error = array('error' => $this->upload->display_errors()); 
     echo "<pre>"; 
     print_r($error); 
     exit(); 
    } else { 
     $fdata = $this->upload->data(); 
     $data['file2'] = 'upload/images' . $fdata['file_name']; 
    } 

    $data['name'] = $this->input->post('dname', TRUE); 
    $data['email'] = $this->input->post('demail', TRUE); 
    $data['mobile'] = $this->input->post('dmobile', TRUE); 
    $data['mobile'] = $this->input->post('daddress', TRUE); 

    $result = $this->insert_model->insert($data); 
    $sdata = array(); 
    $sdata['message'] = "Well done!</strong> You successfully add the 
    Product Details."; 
    $this->session->set_userdata($sdata); 
    redirect('super_admin/add_product', 'refresh'); 
} 
+0

'$データ[ 'FILE2'] = 'アップロード/画像を'。 $ fdata ['file_name']; '' $ data ['file2'] = 'upload/images /'です。 $ fdata ['file_name']; ' – Rits

+0

アップロードディレクトリをチェックインしましたか? 'images'ディレクトリがそこに作成されていますか? – Rits

+0

777のようなアップロード権限を2番目のフォルダに設定しましたか? – Raman

答えて

1

あなたはアップロードの方法を作る必要があります。

/** 
    * Upload a file. 
    * 
    * @param $field 
    * @param $upload_path 
    * @param string $allowed_types 
    * @return bool | file_name 
    */ 
    public function do_upload($field, $upload_path, $allowed_types = 'jpg|png|gif') 
    { 
     $config['upload_path']   = $upload_path; 
     $config['allowed_types']  = $allowed_types; 
     $config['max_size']    = 500; 
     $config['max_width']   = 1024; 
     $config['max_height']   = 1024; 

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

     if (! $this->upload->do_upload($field)) 
     { 
      $this->upload = null; // Unload library 
      return FALSE; 
     } 
     else 
     { 
      $data = $this->upload->data(); 
      $this->upload = null; // Unload library 
      return $data["file_name"]; 
     } 
    } 

これでimage()で使用できます。

public function image() 
{ 
    $data = array(); 

    // For 1st directory 
    if ($data['image'] = $this->do_upload('your_filed_name', 'upload/')) { 
     // Statements 
     // $data['image'] = 'upload/' . $data['image'] 
    } else { 
     $data['errors'] = array('error' => $this->upload->display_errors()); 
    } 

    // For 2st directory 
    if ($data['image'] = $this->do_upload('your_filed_name', 'upload/images/')) { 
     // Statements 
     // $data['image2'] = 'upload/images/' . $data['image'] 
    } else { 
     $data['errors'] = array('error' => $this->upload->display_errors()); 
    } 

    // Your rest of logic 
} 
+0

あなたのコードを試しましたが、まだ整流されていない問題があります –

+0

どうしましたか?エラーが表示されますか? –

+0

以下のコードで解決された問題 –

0

は、次のコードで解決この

public function image() 
{ 
    $data = array(); 
    $error = array(); 
    $config1=array(
     'upload_path'=>'upload/', 
     'allowed_types'=>'jpg|jpeg|png|bmp', 
     'max_size'=>0, 
     'filename'=>url_title($this->input->post('file')) 
    ); 

    $this->load->library('upload',$config1,'file1'); // Create custom object for file 1 upload 
    if($this->file1->do_upload('file')){ 
     $error = array('error' => $this->file1->display_errors()); 
     echo "<pre>"; 
     print_r($error); 
     exit(); 
    }else { 
     $fdata = $this->file1->data(); 
     $data['image'] = 'upload/' . $fdata['file_name']; 
    } 

    $config2=array(
     'upload_path'=>'upload/images/', 
     'allowed_types'=>'jpg|jpeg|png|bmp', 
     'max_size'=>0, 
     'filename'=>url_title($this->input->post('file')) 
    ); 


    $this->load->library('upload', $config2, 'file2'); // Create custom object for file 2 upload 
    $this->file2->initialize($config2); 
    if (!$this->file2->do_upload('file')){ 
     $error = array('error' => $this->file2->display_errors()); 
     echo "<pre>"; 
     print_r($error); 
     exit(); 
    } else { 
     $fdata = $this->file2->data(); 
     $data['file2'] = 'upload/images' . $fdata['file_name']; 
    } 

    $data['name'] = $this->input->post('dname', TRUE); 
    $data['email'] = $this->input->post('demail', TRUE); 
    $data['mobile'] = $this->input->post('dmobile', TRUE); 
    $data['mobile'] = $this->input->post('daddress', TRUE); 

    $result = $this->insert_model->insert($data); 
    $sdata = array(); 
    $sdata['message'] = "Well done!</strong> You successfully add the 
    Product Details."; 
    $this->session->set_userdata($sdata); 
    redirect('super_admin/add_product', 'refresh'); 
} 
+0

私はあなたのコードを試しましたが、私の問題で変更はありません –

+0

どのサーバーを使用していますか? @RamuGampa –

+0

XAMPPからのApache –

0

私の問題を試してみてください:

public function image() 
    {  

      $config['upload_path']   = './upload/images'; 
      $config['allowed_types']  = 'gif|jpg|png'; 
      $config['max_size']    = 0; 
     $this->load->library('upload', $config); 
    if (!$this->upload->do_upload('userfile')){ 
     } else { 
     $fdata = $this->upload->data(); 
    $data['file2'] = 'upload/images' . $fdata['file_name']; 
     } 
      $config2=array(
    'upload_path'=>'upload/', 
    'allowed_types'=>'jpg|jpeg|png|bmp', 
    'max_size'=>0, 
    ); 
    $this->upload->initialize($config2); 

    if($this->upload->do_upload('userfile')){ 
    $data = array(
    'name' =>$this->input->post('name'), 
    'email' =>$this->input->post('email'), 
    'phone' =>$this->input->post('phone'), 
    'title' =>$this->input->post('someField'), 
    'image'=>$this->upload->file_name, 
    ); 
    //Transfering data to Model 
    $this->insert_model->add($data); 
      } 
      else 
      { 
        $data = array('upload_data' => $this->upload->data()); 

        $this->load->view('upload_success', $data); 
      } 
    } 
    } 
関連する問題