2012-01-20 9 views
1

画像をアップロードして画像テーブルに画像を保存しようとしていますが、$this->upload->data()配列から完全なパスを取得する方法はわかりませんが、これは以下のコードです。image codeigniterでアップロードする

//assigns the user's sessioned id to the variable $user_id 

$user_id = $this->session->userdata('user_id'); 

//sets rules for the image that is being uploaded 
$config['overwrite'] = FALSE; //does not overwrite any image adds +1 instead 
$config['encrypt_name'] = FALSE; //encrypts the images name 
$config['remove_spaces'] = TRUE; //removes spaces from the images name 
$config['file_name'] = $user_id."_0.jpg";/*gives the image a new name combination of the user's id and a number*/ 
$config['upload_path'] = './uploads'; // the uploaded images path 
$config['allowed_types'] = 'jpg|png';/*types of image extentions allowed to be uploaded*/ 
$config['max_size'] = '2048';// maximum file size that can be uploaded (2MB) 

if (! is_dir($config['upload_path'])) /* this checks to see if the file path is wrong or does not exist.*/ 
    die("THE UPLOAD DIRECTORY DOES NOT EXIST"); // error for invalid file path 

$this->load->library('upload',$config); /* this loads codeigniters file upload library*/ 
//this checks for errors incase the image upload breaks a set rule. 
if (! $this->upload->do_upload()) { 
    echo "UPLOAD ERROR ! ".$this->upload->display_errors(); //image error 
} 
else { 
    // success message to show that the image was successfuly uploaded. 
    echo "THE IMAGE HAS BEEN UPLOADED : "; var_dump($this->upload->data()); 
} 

答えて

0

あなたは$this->upload->data()次の配列のためのダンプに見てする必要があります。

Array 
(
    [file_name] => mypic.jpg 
    [file_type] => image/jpeg 
    [file_path] => /path/to/your/upload/ 
    [full_path] => /path/to/your/upload/jpg.jpg 
    [raw_name]  => mypic 
    [orig_name] => mypic.jpg 
    [client_name] => mypic.jpg 
    [file_ext]  => .jpg 
    [file_size] => 22.2 
    [is_image]  => 1 
    [image_width] => 800 
    [image_height] => 600 
    [image_type] => jpeg 
    [image_size_str] => width="800" height="200" 
) 

だから、あなたは、単にのようなものを使用することができます

if ($uploaded_image = $this->upload->do_upload()){ 
    // success message to show that the image was successfuly uploaded. 
    echo "THE IMAGE HAS BEEN UPLOADED : "; 
    $full_path = $uploaded_image['full_path']; 
} else { 
    echo "UPLOAD ERROR ! ".$this->upload->display_errors();//image error 
} 
+0

これは何も表示されませんでした!画像はアップロードされましたが、完全なパスは表示されませんでした。 –

+0

if文のどの部分が終わるのですか? – simnom

+0

if($ uploaded_image = $ this-> upload-> do_upload()){ //画像が正常にアップロードされたことを示す成功メッセージ。 エコー「画像がアップロードされました:」; –

2

を私が行うために必要なすべてを保存することでした変数内の配列$this->upload->data();は、たとえば

$image_info = $this->upload->data(); 

および$image_infoから、私はそのように画像プロパティにアクセスできます。

$image_info['full_path']; 
$image_info['file_name']; 
関連する問題