在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
中的文件夹存在并且有写权限。