以下是我尝试上传文件的代码。我得到的错误是
You did not select a file to upload.
请帮我解决这个问题如何解决这个问题
<?php echo form_open_multipart('garage/do_upload');?>
<input type="file" name="userfile" />
<input type="submit" value="upload" />
</form>
do_upload功能
public function do_upload() {
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];
$this->upload->initialize($this->set_upload_options());
if($this->upload->do_upload()){
echo 'Done';
die;
}
else {
print_r($this->upload->display_errors());
}
}
}
private function set_upload_options()
{
//upload an image options
$config = array();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000000000000';
$config['overwrite'] = FALSE;
return $config;
}
我将使用此代码进行多个图片上传,但目前它不适用于单张图片,请帮助
答案 0 :(得分:0)
public function do_upload()
{
$error = NULL;
// check permission of upload_path
// I recommended that user fullpath
$this->load->library('upload', array(
'delete_origin' => TRUE,
'upload_path' => './uploads/',
'remove_spaces' => TRUE,
'file_ext_tolower' => TRUE,
'allowed_types' => 'jpg|png|jpeg',
'max_size' => '4096', // 1024 KB => 1 MB
'max_width' => '1920',
'max_height' => '1080',
));
if( ! $this->upload->do_upload('userfile') )
{
$error = $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
}
return ( empty( $error ) ) ? $data : $error;
}