0

新しい HTML は次のとおりです。

<input type="file" name="file_1" />
<input type="text" name="image_description_1" class="text-input"/>

新しい _submit 関数は次のとおりです。

if($this->CI->input->post('file_1')){
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'jpg|jpeg|gif|png';
$config['max_size'] = 2000;
$config['upload_path'] = realpath(APPPATH . '../assets/uploads/avatars');

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

$image_data = $this->CI->upload->data();

$image['description'] = $this->CI->input->post('image_description_1');
$image['user_id'] = $id;
$image['image'] = $image_data['file_name'];

$this->CI->db->insert('report_images',$image);

}

説明と user_id は正しく送信されていますが、ファイルが失われています。

私は何か違うことをすべきですか?何が問題なのかよくわかりません。

4

3 に答える 3

2

私はCodeigniterのアップロードクラスをあなたが必要とするものにぴったり合うように拡張しました。このクラスでは、validate_uploadとdo_uploadの2つのメソッドを定義しました。このファイルには新しいコードを記述しませんでした。代わりに、do_uploadコードを2つの部分に分割しました。validate_uploadはアップロードを検証し、ファイルが検証されていない場合はfalseを返します。また、do_uploadは、validate_uploadがtrueを返した場合にのみ使用する必要があります。これがコードです。

Class My_Upload extends CI_Upload
{   

    public function __construct(){
         parent::__construct();
    }   

    public function validate_upload($field = 'userfile')
    {

        // Is $_FILES[$field] set? If not, no reason to continue.
        if ( ! isset($_FILES[$field]))
        {
            $this->set_error('upload_no_file_selected');
            return FALSE;
        }

        // Is the upload path valid?
        if ( ! $this->validate_upload_path())
        {
            // errors will already be set by validate_upload_path() so just return FALSE
            return FALSE;
        }

        // Was the file able to be uploaded? If not, determine the reason why.
        if ( ! is_uploaded_file($_FILES[$field]['tmp_name']))
        {
            $error = ( ! isset($_FILES[$field]['error'])) ? 4 : $_FILES[$field]['error'];

            switch($error)
            {
                case 1: // UPLOAD_ERR_INI_SIZE
                    $this->set_error('upload_file_exceeds_limit');
                    break;
                case 2: // UPLOAD_ERR_FORM_SIZE
                    $this->set_error('upload_file_exceeds_form_limit');
                    break;
                case 3: // UPLOAD_ERR_PARTIAL
                    $this->set_error('upload_file_partial');
                    break;
                case 4: // UPLOAD_ERR_NO_FILE
                    $this->set_error('upload_no_file_selected');
                    break;
                case 6: // UPLOAD_ERR_NO_TMP_DIR
                    $this->set_error('upload_no_temp_directory');
                    break;
                case 7: // UPLOAD_ERR_CANT_WRITE
                    $this->set_error('upload_unable_to_write_file');
                    break;
                case 8: // UPLOAD_ERR_EXTENSION
                    $this->set_error('upload_stopped_by_extension');
                    break;
                default :   $this->set_error('upload_no_file_selected');
                    break;
            }

            return FALSE;
        }


        // Set the uploaded data as class variables
        $this->file_temp        =   $_FILES[$field]['tmp_name'];
        $this->file_size        =   $_FILES[$field]['size'];
        $this->_file_mime_type($_FILES[$field]);
        $this->file_type        =   preg_replace("/^(.+?);.*$/", "\\1", $this->file_type);
        $this->file_type        =   strtolower(trim(stripslashes($this->file_type), '"'));
        $this->file_name        =   $this->_prep_filename($_FILES[$field]['name']);
        $this->file_ext     =   $this->get_extension($this->file_name);
        $this->client_name  =   $this->file_name;

        // Is the file type allowed to be uploaded?
        if ( ! $this->is_allowed_filetype())
        {
            $this->set_error('upload_invalid_filetype');
            return FALSE;
        }

        // if we're overriding, let's now make sure the new name and type is allowed
        if ($this->_file_name_override != '')
        {
            $this->file_name = $this->_prep_filename($this->_file_name_override);

            // If no extension was provided in the file_name config item, use the uploaded one
            if (strpos($this->_file_name_override, '.') === FALSE)
            {
                $this->file_name .= $this->file_ext;
            }

            // An extension was provided, lets have it!
            else
            {
                $this->file_ext  = $this->get_extension($this->_file_name_override);
            }

            if ( ! $this->is_allowed_filetype(TRUE))
            {
                $this->set_error('upload_invalid_filetype');
                return FALSE;
            }
        }

        // Convert the file size to kilobytes
        if ($this->file_size > 0)
        {
            $this->file_size = round($this->file_size/1024, 2);
        }

        // Is the file size within the allowed maximum?
        if ( ! $this->is_allowed_filesize())
        {
            $this->set_error('upload_invalid_filesize');
            return FALSE;
        }

        // Are the image dimensions within the allowed size?
        // Note: This can fail if the server has an open_basdir restriction.
        if ( ! $this->is_allowed_dimensions())
        {
            $this->set_error('upload_invalid_dimensions');
            return FALSE;
        }

        // Sanitize the file name for security
        $this->file_name = $this->clean_file_name($this->file_name);

        // Truncate the file name if it's too long
        if ($this->max_filename > 0)
        {
            $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename);
        }

        // Remove white spaces in the name
        if ($this->remove_spaces == TRUE)
        {
            $this->file_name = preg_replace("/\s+/", "_", $this->file_name);
        }

        /*
         * Validate the file name
         * This function appends an number onto the end of
         * the file if one with the same name already exists.
         * If it returns false there was a problem.
         */
        $this->orig_name = $this->file_name;

        if ($this->overwrite == FALSE)
        {
            $this->file_name = $this->set_filename($this->upload_path, $this->file_name);

            if ($this->file_name === FALSE)
            {
                return FALSE;
            }
        }

        /*
         * Run the file through the XSS hacking filter
         * This helps prevent malicious code from being
         * embedded within a file.  Scripts can easily
         * be disguised as images or other file types.
         */
        if ($this->xss_clean)
        {
            if ($this->do_xss_clean() === FALSE)
            {
                $this->set_error('upload_unable_to_write_file');
                return FALSE;
            }
        }
        $this->set_image_properties($this->upload_path.$this->file_name);
        return TRUE;
    }

    public function do_upload($field = 'userfile')
    {
        /*
         * Move the file to the final destination
         * To deal with different server configurations
         * we'll attempt to use copy() first.  If that fails
         * we'll use move_uploaded_file().  One of the two should
         * reliably work in most environments
         */
        if ( ! @copy($this->file_temp, $this->upload_path.$this->file_name))
        {
            if ( ! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name))
            {
                $this->set_error('upload_destination_error');
                return FALSE;
            }
        }

        /*
         * Set the finalized image dimensions
         * This sets the image width/height (assuming the
         * file was an image).  We use this information
         * in the "data" function.
         */
        return TRUE;    
    }
}   
于 2012-10-22T07:06:09.310 に答える
2

このフォーム検証拡張機能は、アップロード前にファイルを検証するのに役立ちます。許可されている最小サイズと最大サイズ、許可されている有効なファイルの種類、画像の最小幅と最大幅と高さを確認できます。このライブラリをライブラリ フォルダーにドロップするだけで、拡張機能のドキュメントに従ってルールを設定します。

MY_Form_validation

于 2012-10-22T06:59:21.170 に答える
0

do_upload選択した一時ディレクトリや/tmp、必要な処理を行うだけではありません。合格した場合は、行を作成し、ファイルをディレクトリに保存します。

于 2012-10-22T04:54:45.657 に答える