Codeigniter文件上传错误:'您没有选择要上传的文件'
问题描述:
我正在使用codeigniter 3,在3210函数中得到如You did not select a file to upload.
的错误,但正常的php函数move_uploaded_file()
正常工作。我提到了大多数来自stackoverflow的答案,但我没有得到解决方案。Codeigniter文件上传错误:'您没有选择要上传的文件'
我认为这可能在wamp
问题,但我没有弄清楚它在哪里。 如果这段代码在你的机器上工作,那么它将在我的wamp php或Apache设置中出现。
查看:(upload.php的)
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php echo form_open_multipart('welcome/do_upload'); ?>
<input id="sfile" type="file" name="file">
<input type="submit" value="Submit">
<?php echo form_close(); ?>
</body>
</html>
控制器:(的welcome.php)
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (! $this->upload->do_upload()) //not working
{
$error = array('error' => $this->upload->display_errors());
var_dump($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
}
echo $basename = $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'],$config['upload_path'].$basename);
// move_uploaded_file() works fine
}
答
按照CI 3,你需要通过名称属性,在$this->upload->do_upload('')
。
所以你需要通过name属性在文件中它
$this->upload->do_upload('file');
检查大小,你已经上传,因为你设置$ config['max_size'] = '100';
。这意味着你将无法上传文件的大小为100多个KB
答
先装上载库,然后初始化后的配置数据。
并在$ this-> upload-> do_upload('file')中传递输入文件类型的属性名称。
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config); # Load upload library
$this->upload->initialize($config); # Initialize
if (! $this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
var_dump($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
}
}
尝试在'$这个 - > upload-> do_upload(“文件”)',并检查 – Saty
@Saty是的,我尝试过同样的问题 – 151291
检查文件的大小你,因为你设置上传'$通过字段名config ['max_size'] ='100';'。这意味着你不能哟上传文件大小超过100 kb – Saty