当我尝试从帮助程序文件夹中使用代码点火器的上传库时,任何人都可以提供帮助,但我不断收到相同的错误,即我没有选择要上传的图像? 以前有人尝试过吗?
class FileUpload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'file_uploading'));
$this->load->library('form_validation', 'upload');
}
public function index() {
$data = array('title' => 'File Upload');
$this->load->view('fileupload', $data);
}
public function doUpload() {
$submit = $this->input->post('submit');
if ( ! isset($submit)) {
echo "Form not submitted correctly";
} else { // Call the helper
if (isset($_FILES['image']['name'])) {
$result = doUpload($_FILES['image']);
if ($result) {
var_dump($result);
} else {
var_dump($result);
}
}
}
}
}
帮助程序函数
<?php
function doUpload($param) {
$CI = &get_instance();
$CI->load->library('upload');
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|png|jpg|jpeg|png';
$config['file_name'] = date('YmdHms' . '_' . rand(1, 999999));
$CI->upload->initialize($config);
if ($CI->upload->do_upload($param['name'])) {
$uploaded = $CI->upload->data();
return $uploaded;
} else {
$uploaded = array('error' => $CI->upload->display_errors());
return $uploaded;
}
}
您的代码中有一些小错误,请按如下方式修复,
$result = doUpload($_FILES['image']);
在这里,您应该根据您的代码传递表单字段名称image
是文件输入的名称。
所以你的代码应该是这样的
$result = doUpload('image');
然后,在函数doUpload
中,您应该更新代码 从
$CI->upload->do_upload($param['name'])
自
$CI->upload->do_upload($param)
因为表单字段的名称应传递给do_upload
函数才能成功上传文件。
注意
确保您在表单中添加了
enctype="multipart/form-data"
元素