0

複数の画像をアップロードしようとしています。私は Codeigniter を使用しており、ファイルアップロードクラスが組み込まれていることは知っていますが、カスタムメイドの画像アップロードライブラリを試しているところです。以下のコードを提供しました。

次のコードで直面している問題は、1 つだけ (フォームで最後に選択されたもの) の画像をアップロードしていることです。

どこが間違っているのか教えてください。

私のコントローラー:

function img_upload(){

   $this->load->library('image_upload');         

    $image1= $this->image_upload->upload_image('imagefile1');
    $image2= $this->image_upload->upload_image('imagefile2');
    $image3= $this->image_upload->upload_image('imagefile3');

   echo $image1 ."<br>"; echo $image2;  
}

application/libraries/image_upload.php (自作ライブラリ)

  function upload_image($image_info){

    $image=$_FILES[$image_info]['name'];
    $filename = stripslashes($image);
    $extension = $this->getExtension($filename);
    $extension = strtolower($extension);

    $image_name=time().'.'.$extension;

   $newname="support/images/products/".$image_name;
   $uploaded = move_uploaded_file($_FILES[$image_info]['tmp_name'], $newname);

   if($uploaded) {return $image_name; } else {return FALSE;}
} 

マイフォーム

 <form id="myForm" enctype="multipart/form-data" 
 action="<?php echo base_url();?>add/img_upload" method="post" name="myForm">

  <input type="file" name="imagefile1" size="20" /><br>
  <input type="file" name="imagefile2" size="20" /><br>
  <input type="file" name="imagefile3" size="20" /><br>
   <br /><br />
  <input type="submit" value="upload" />    
 </form>  
4

2 に答える 2

0

私は自分の問題の解決策を見つけ、共有すれば誰かを助けることができると思いました。

問題は、image_upload.phpファイル(カスタムメイドのライブラリ)、特に次の行にありました。

 $image_name=time().'.'.$extension; // 

time()はおそらくファイルを上書きしていました。そこで、以前のコードを次のように置き換えました。

function upload_image($image_info){

     $image=$_FILES[$image_info]['name'];
     $filename = stripslashes($image);
     $extension = $this->getExtension($filename);
     $extension = strtolower($extension);

     $image_name=$this->get_random_number().'.'.$extension;

     $newname="support/images/products/".$image_name;
     $uploaded = move_uploaded_file($_FILES[$image_info]['tmp_name'], $newname);

  if($uploaded) {return $image_name; } else {return FALSE;}
 }


  function get_random_number(){

    $today = date('YmdHi');
    $startDate = date('YmdHi', strtotime('-10 days'));
    $range = $today - $startDate;
    $rand1 = rand(0, $range);
    $rand2 = rand(0, 600000);
    return  $value=($rand1+$rand2);
}
于 2012-10-13T08:15:13.610 に答える
0

ある種のロールバック機能を作成し、CI ネイティブ lib を使用できます。サーバーにとって余分な作業はほとんどありませんが、少ない/クリーン/簡単なTOdebugコードであり、機能します.

function do_upload()
{

1 - 構成

    $config['upload_path'] = './uploads/';
    // other configurations 

    $this->load->library('upload', $config);
    $error = array('stat'=>0 , 'reason'=>'' ;

2-アップロード

        if ( ! $this->upload->do_upload('file_1'))
        {
            $error['stat'] = 1 ;
            $error['reason'] = $this->upload->display_errors() ;

        }
        else
        { $uploaded_array[] = $uploaded_file_name ; } 

            // you may need to clean and re initialize library before new upload 
        if ( ! $this->upload->do_upload('file_2'))
        {
            $error['stat'] = 1 ;
            $error['reason'] = $this->upload->display_errors() ;

        }
        else
        { $uploaded_array[] = $uploaded_file_name ; } 

3 - エラーのチェックと最後にロールバック

if($error['stat'] == 1 )
{   
    $upload_path = './upload/';
    if(!empty($uploaded_array))
    {
        foreach( $uploaded_array as $uploaded )
        { 
           $file = $upload_path.$uploaded;
           if(is_file($file))
           unlink($file);
        }
    }
    echo 'there was a problem : '.$error['reason'];
}
else
{
   echo 'success';
}




}
于 2012-10-13T07:04:57.137 に答える