2017-02-16 63 views
-1

我知道這可能是錯的問,但是當我插入多個圖像使用Ajax並將其插入到數據庫文件越來越插入,但鏈接有錯讓我告訴你當我上傳多個圖像到我的數據庫使用codeigniter文件url是錯誤的,但圖像插入文件夾和數據庫

「的」 http://localhost/P_Display/uploads/bike-926023_192048.jpg「」

這是我從控制檯得到的URL文件正在保存,但可能是因爲我正在使用for循環來上傳多個文件

讓我告訴你控制器代碼

public function post_data_multimage() 
{ 
$data = array(); 
    $data['title'] = 'Multiple file upload'; 
    if($this->input->post()) 
    { 
    // retrieve the number of images uploaded; 
    $number_of_files = sizeof($_FILES['multimage']['tmp_name']); 
    $files = $_FILES['multimage']; 
    $errors = array(); 

for($i=0;$i<$number_of_files;$i++) 
    { 
     if($_FILES['multimage']['error'][$i] != 0) $errors[$i][] = 'Couldnt upload file '.$_FILES['multimage']['name'][$i]; 
    } 
    if(sizeof($errors)==0) 
    { 
     // now, taking into account that there can be more than one file, for each file we will have to do the upload 
     // we first load the upload library 
     $this->load->library('upload'); 
     // next we pass the upload path for the images 
     $config['upload_path'] = FCPATH . './uploads/'; 
     // also, we make sure we allow only certain type of images 
     $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
     for ($i = 0; $i < $number_of_files; $i++) { 
     $_FILES['multimage']['name'] = $files['name'][$i]; 
     $_FILES['multimage']['type'] = $files['type'][$i]; 
     $_FILES['multimage']['tmp_name'] = $files['tmp_name'][$i]; 
     $_FILES['multimage']['error'] = $files['error'][$i]; 
     $_FILES['multimage']['size'] = $files['size'][$i]; 
     //now we initialize the upload library 
     $this->upload->initialize($config); 

     $image= $_FILES['multimage']['name']; 

     $post = $this->input->post(); 

      unset($post['submit']); 

     $this->load->model('Pmodel'); 
     $multi_data= $this->Pmodel->post_data_multimage($post,$image); 


     // we retrieve the number of files that were uploaded 
     if ($this->upload->do_upload('multimage')) 
     { 
      $data['uploads'][$i] = $this->upload->data(); 
      $upload_data=$data['uploads'][$i]; 
      $image_path= base_url("uploads/".$upload_data['raw_name'].$upload_data['file_ext']); 
     echo json_encode($image_path); 
     } 
     else 
     { 
      $data['upload_errors'][$i] = $this->upload->display_errors(); 
     } 
     } 
    } 
     else 
    { 
     print_r($errors); 
    } 
    echo '<pre>'; 
    print_r($data); 
    echo '</pre>'; 
    } 
    else 
    { 
    echo "string"; 
    } 
} 

如何從我的網址中刪除這些斜槓?

回答

1

我想這可能工作我們需要的是一個HTML元素添加到從控制器回聲圖像控制器,我們不需要值傳遞給這樣的觀點結束此錯誤的URL

的衝突是我的代碼

$data = array(); 
    $data['title'] = 'Multiple file upload'; 
    if($this->input->post()) 
    { 
    // retrieve the number of images uploaded; 
    $number_of_files = sizeof($_FILES['multimage']['tmp_name']); 
    $files = $_FILES['multimage']; 
    $errors = array(); 

for($i=0;$i<$number_of_files;$i++) 
    { 
     if($_FILES['multimage']['error'][$i] != 0) $errors[$i][] = 'Couldnt upload file '.$_FILES['multimage']['name'][$i]; 
    } 
    if(sizeof($errors)==0) 
    { 
     // now, taking into account that there can be more than one file, for each file we will have to do the upload 
     // we first load the upload library 
     $this->load->library('upload'); 
     // next we pass the upload path for the images 
     $config['upload_path'] = FCPATH . './uploads/'; 
     // also, we make sure we allow only certain type of images 
     $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
     for ($i = 0; $i < $number_of_files; $i++) { 
     $_FILES['multimage']['name'] = $files['name'][$i]; 
     $_FILES['multimage']['type'] = $files['type'][$i]; 
     $_FILES['multimage']['tmp_name'] = $files['tmp_name'][$i]; 
     $_FILES['multimage']['error'] = $files['error'][$i]; 
     $_FILES['multimage']['size'] = $files['size'][$i]; 
     //now we initialize the upload library 
     $this->upload->initialize($config); 

     $image= $_FILES['multimage']['name']; 

     $post = $this->input->post(); 

      unset($post['submit']); 

     $this->load->model('Pmodel'); 
     $multi_data= $this->Pmodel->post_data_multimage($post,$image); 


     // we retrieve the number of files that were uploaded 
     if ($this->upload->do_upload('multimage')) 
     { 
      $data['uploads'][$i] = $this->upload->data(); 
      $upload_data=$data['uploads'][$i]; 

      $user_image = array('src' => base_url("uploads/" .$image), 
                  'width' => '100px', 
                  'height'=> '100px', 
                  ); 
     echo "<a href='#'>" . img($user_image)."</a>"; 

     } 
     else 
     { 
      $data['upload_errors'][$i] = $this->upload->display_errors(); 
     } 
     } 
    } 
     else 
    { 
     print_r($errors); 
    } 

    } 
    else 
    { 
    echo "string"; 
    } 
} 

看,我只是呼應了圖像與因而其路徑排除故障。

0

是否存在保存一樣的

'Assets/image_upload/'.$image= $_FILES['multimage']['name']

數據庫的場圖像表中的地址。

當您顯示它。

echo(base_url($item->image))//image field showed in database.

+0

這是我的解決方案。 –

+0

您的解決方案是正確的,但我不需要將地址添加到我的數據庫我只想顯示我的圖像,並可能發生沒有圖像路徑插入。 –

+0

您可以保存base_url('Assets/image_upload /')。 $ image = $ _FILES ['multimage'] ['name']到數據庫字段中。 –

相關問題