我一直在谷歌上搜索2-3个小时,查看与此相关的每个stackoverflow线程.据我所见,很多人都有同感.像许多其他人一样,我无法上传图像,即使它没有显示任何错误.
我真的尝试过任何我在网上找到的东西,但似乎没有什么工作……
我只能用var_dump($_ FILES)看到图像的属性.我想将图像存储到文件夹中,并将其名称存储到数据库中.
调节器
function upload_sl() { $config['upload_path'] = 'sliki/samurajsliki/'; $config['allowed_types'] = 'gif|jpg|png'; $config['max_size'] = '10000'; $this->load->library('upload',$config); /*$this->upload->initialize($config);*/ /*var_dump($_FILES);*/ if ( ! $this->upload->do_upload()) { $error = array('error' => $this->upload->display_errors()); $this->load->view('samuraj/slikaclen',$error); } else { $data = array('upload_data' => $this->upload->data()); $this->load->view('header_samurajmain'); $this->load->view('samuraj/admin_samuraj'); $this->load->view('footer_subpage'); } }
视图
<?PHP echo $error;?> <div class="center-block" style="width: 30%; height: 50%; clear:both;"> <?PHP echo form_open_multipart('admin_page/upload_sl');?> <input type="file" name="userfile" size="20" /> <br /> <input type="submit" value="Стави нова слика" /> </form> </div>
我在档案中找到了这个.我不知道它的工作原理.
也许你想尝试一下.
也许你想尝试一下.
<form action="admin/#/add_content/do_upload" enctype="multipart/form-data" method="post" accept-charset="utf-8"> <input type="file" class="mb5" name="userfiles[]" multiple> </form>
这是控制器.
<?PHP defined('BASEPATH') OR exit('No direct script access allowed'); class Upload extends CI_Controller { public function __construct(){ parent::__construct(); } public function do_upload() { $config['upload_path'] = './uploads/'; $config['allowed_types'] = 'gif|jpg|png'; $this->load->library('upload',$config); $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]; if($this->upload->do_upload()){ echo "File ".$_FILES["userfile"]["name"]. " has been uploaded.\n"; } else{ //Error. this->upload->display_errors(); } } header("Location: http://localhost:82/MyCRM/admin/#/add_content"); } }