1

サーバーにアップロードされる画像のサムネイルを作成しようとしています。https://www.codeigniter.com/userguide3/libraries/image_lib.html#processing-an-imageを参照してください。元の画像とサムネイルが同じ場所に保存されるため、作成後、サムネイルを別のフォルダーに保存したいと考えています。

そのため、関数を使用してみmove_uploaded_file()ましたが、ファイルを移動できません。コードが正しく実行されているかどうかはよくわかりません。見てみな。

Belwo は私のコードです:

if($_FILES['file_name']['size'] != 0){
    $config['upload_path']          = FCPATH.MEDIA_LIB_URI.'facts';
    $config['allowed_types']        = 'jpg|jpeg|png|gif';
    $config['max_size']             = 0;

    $newFileName = removeExt($_FILES['file_name']['name']).'-'.uniqueId();      // renaming the file name without the ext
    $new_name = $newFileName.getExt($_FILES['file_name']['name']);              // new file name with the ext
    $config['file_name'] = $new_name;

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

    if($this->upload->do_upload('file_name')){
        $uploadData = $this->upload->data();
        $file_name = $uploadData['file_name'];

        $this->load->library('image_lib');
        $configer = array(
            'image_library'   => 'gd2',
            'source_image'    => $uploadData['full_path'],
            'create_thumb'    => TRUE,
            'maintain_ratio'  => TRUE,
            'width'           => 950,
            'height'          => 950,
        );

        $this->image_lib->clear();
        $this->image_lib->initialize($configer);
        $this->image_lib->resize();

        $thumbName = $newFileName.'_thumb'.getExt($_FILES['file_name']['name']);    // getting the exact thumbnail name that been created by codeigniter
        move_uploaded_file($thumbName, FCPATH.MEDIA_LIB_URI.'facts/thumbnail/'.$new_name);

        return $file_name;
    } else {
        return $this->upload->display_errors();
    }
}
4

2 に答える 2