0

ページに単一のフォームがあります。5 つのテキスト フィールドと 3 つのアップロード ファイル フィールドがあります。テキストとファイルのパスをデータベースに書き込む必要があります。オンラインで多くの例を見てきましたが、ほとんどは単一のファイルをアップロードするか、同じアップロード フィールドから複数のファイルをアップロードするものです。

私は CodeIgniter を初めて使用するので、コード スニペットは非常に役に立ちます。

よろしくお願いします。

4

2 に答える 2

1

別の提案は次のとおりです。

function upload()
{
    $config['upload_path'] = $path; //$path=any path you want to save the file to...
    $config['allowed_types'] = 'gif|jpg|png|jpeg'; //this is the file types allowed
    $config['max_size'] = '1024'; //max file size

    $config['max_width']  = '1024';//if file type is image
    $config['max_height']  = '768';//if file type is image

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

    foreach($_FILES as $Key => $File)
    {
        if($File['size'] > 0)
        {
            if($this->upload->do_upload($Key))
            {
                $data = $this->upload->data();
                echo $data['file_name'];
            }
            else
            {
                // throw error
                echo $this->upload->display_errors();
            }
        }
    }
}

これは、投稿するすべてのファイル入力に対して自動的に機能し、名前や数量は関係ありません:)

于 2012-08-29T10:06:29.237 に答える
1

お役に立てれば

$config['upload_path'] = $path; //$path=any path you want to save the file to...
$config['allowed_types'] = 'gif|jpg|png|jpeg'; //this is the file types allowed
$config['max_size'] = '1024'; //max file size

$config['max_width']  = '1024';//if file type is image
$config['max_height']  = '768';//if file type is image
//etc config for file properties, you can check all of them out on website

ここで、1.jpg、2.jpg、3.gifとして保存したい 3 つのファイルがあり、3 つの入力フィールドpic1、pic2、pic3を介してアップロードされるとします。

for($ite=1;$ite<=3;$ite++){
    if(!empty($_FILES["pic".$ite]["name"])){ //if file is present
        $ext = pathinfo($_FILES['pic'.$ite]['name'], PATHINFO_EXTENSION); //get extension of file
        $config["file_name"]="$ite.$ext"; //rename file to 1.jpg,2.jpg or 3.jpg, depending on file number and its extension

        $this->upload->initialize($config); //upload library of codeigniter initialize function with config properties set earlier
        if(!$this->upload->do_upload("pic".$ite)){
            //error code

        }

        }
    }
于 2012-08-29T01:20:58.597 に答える