我在CodeIgniter 3中多次上传图片时遇到问题。我尝试了MY_Upload Class
,我尝试了其他代码,但没有一个适用于我。
控制器
function do_uploadMultiple(){
$config['upload_path'] = './assets/page/img/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$nameField = 'imagem';
if ( ! $this->upload->do_upload($nameField)){
$error = array('error' => $this->upload->display_errors());
}else{
$foto = array('upload_data' => $this->upload->data());
return $foto['upload_data']['file_name'];
}
}
public function cadastrarPage(){
$data['nome_menu'] = $this->input->post('nome');
$data['titulo'] = $this->input->post('titulo');
$data['text'] = $this->input->post('text');
$data['video'] = $this->input->post('video');
$teste['imagem'] = $this->input->post('imagem');
$id_abrase = $this->Abrase_model->cadastrarPage($data);
for($i = 0; $i < count($_FILES['imagem']['name']); $i++){
$img['id_abrase'] = $id_abrase;
$img['imagem'] = $this->do_uploadMultiple();
$result = $this->Abrase_model->gravarImagem($img);
if($result){
echo 'yeah';
}else{
echo 'fail';
}
}
}
查看
<form method="post" action="<?php echo base_url('index.php/abrase/cadastrarPage'); ?>" enctype="multipart/form-data">
<div class="form-group">
<label for="nome">Nome da página no menu</label>
<input id="nome" name="nome" type="text" class="form-control">
</div>
<div class="form-group">
<label for="titulo">Título</label>
<input id="titulo" name="titulo" type="text" class="form-control">
</div>
<div class="form-group">
<label for="text">Texto da Página</label>
<textarea class="form-control" name="text" id="text" cols="30" rows="10"></textarea>
</div>
<div class="form-group">
<label for="imagem">Selecione as imagens </label>
<input class="file" name="imagem[]" id="imagem" type="file" multiple data-show-upload="false">
</div>
<div class="form-group">
<label for="video">Vídeo</label>
<input type="text" name="video" id="video" class="form-control">
</div>
<div class="form-group">
<label for="pdf">Arquivos</label>
</div>
<div class="form-group">
<input class="btn btn-info" type="submit" value="Cadastrar">
</div>
</form>
模型
public function cadastrarPage($data){
$this->db->set('data_add', 'NOW()', FALSE);
$this->db->set('excluido', 0);
$this->db->insert('abrase', $data);
return $this->db->insert_id();
}
public function gravarImagem($data){
return $this->db->insert('abrase_img', $data);
}
现在错误
A PHP Error was encountered
Severity: Warning
Message: is_uploaded_file() expects parameter 1 to be string, array given
Filename: libraries/Upload.php
Line Number: 412
Backtrace:
File: C:\xampp\htdocs\Tattica\ABRASE\application\controllers\Abrase.php
Line: 32
Function: do_upload
File: C:\xampp\htdocs\Tattica\ABRASE\application\controllers\Abrase.php
Line: 54
Function: do_uploadMultiple
File: C:\xampp\htdocs\Tattica\ABRASE\index.php
Line: 315
Function: require_once
我尝试了我在网络上找到的所有内容,但无法做到。我需要帮助。
答案 0 :(得分:1)
我遇到了同样的问题,Codeigniter实际上无法处理多个文件上传,多个上传类是针对较旧的codeigniter,但这里有一个你可以使用的代码,这使用本机PHP代码,据我所知,这个是在codeigniter中上传多个的唯一方法。
foreach($_FILES['file']['tmp_name'] as $index => $f)
{
$target_dir = './assets';
$target_file = $target_dir . basename($_FILES["file"]["name"][$index]);
$uploadOk = 1;
$files_name = $_FILES["file"]["name"][$index];
// Check if file already exists
if (file_exists($target_file))
{
//Filexists
$uploadOk = 0;
}
// Check file size
if ($_FILES["file"]["size"][$index] > 5000000)
{
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0)
{
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
}
else
{
if (move_uploaded_file($_FILES["file"]["tmp_name"][$index], $target_file))
{
//success
}
else
{
echo "Sorry, there was an error uploading your file.";
}
}
}