PHP-CI 檔案上傳

摘要:PHP-CI 檔案上傳

參考網址:

File Uploading 類別

http://codeigniter.org.tw/user_guide/libraries/file_uploading.html

CodeIgniter文件上传错误:the filetype you are attempting to upload is not allowed

http://www.php230.com/codeigniter-upload-file-error-the-filetype-you-are-attempting-to-upload-is-not-allowed.html

分享CodeIgniter上传图片成功的全过程

http://www.cnblogs.com/ly312/archive/2011/06/21/2086167.html

 

html頁面

<form > 標籤要加上enctype="multipart/form-data"

要建立input

<input name="file_path" type="file" id="file_path">

 

PHP程式

建立upload config

        $config['upload_path'] = './upload/'; //存放路徑
        $config['allowed_types'] = '*'; //允許所有副檔名
        $config['overwrite']     = true;
        $config['file_name']  = time(); //文件名不使用原始名 (避免中文檔名錯誤)

        $this->load->library('upload',$config);
        $field_name = "file_path";

        if (!$this->upload->do_upload($field_name)) {
            //失敗
            echo $this->upload->display_errors();
        } else {
            //成功
            $file_data = $this->upload->data();
            echo json_encode($file_data);
        }