在PHP CodeIgniter中上傳文件,可以使用以下步驟:
config
文件夾中的autoload.php
文件中加載upload
庫:$autoload['libraries'] = array('upload');
<form action="upload_file" method="post" enctype="multipart/form-data">
<input type="file" name="userfile">
<input type="submit" value="Upload">
</form>
public function upload_file() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = 1000;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
} else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
echo "File uploaded successfully.";
通過以上步驟,就可以在PHP CodeIgniter中實現文件上傳功能。需要注意的是,要確保upload_path
中的文件夾存在并且有寫權限。