gpt4 book ai didi

mysql - 在 codeigniter 中上传后获取文件名作为数组

转载 作者:行者123 更新时间:2023-11-29 11:15:01 24 4
gpt4 key购买 nike

我在上传文件后返回文件名时遇到问题。当我按下提交按钮时,它会将图像上传到文件夹,但我没有获取文件名。

这是代码:

Controller :

public function add()
{
$title = $this->input->post('title');
$files = $this->do_upload();
//print_r($files);
$this->user->in($title,$files);
}


public function do_upload()
{

$name_array = array();
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<=$cpt-1; $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());
$data = $this->upload->do_upload();
$name_array[] = $data['file_name'];
}

$names = implode(',', $name_array);
return $names;
}

private function set_upload_options()
{
$config = array();
$config['upload_path'] = './portfolio/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;

return $config;
}

型号:

public function in($title,$file)
{
foreach ($title as $key => $n) {

$insert[] = array(
'title' => $n,
'file' => $file[$key]
);
}
$this->db->insert_batch('title',$insert);
}

查看:

<?php echo form_open_multipart('location/add'); ?>
<div>
<input type="text" name="title[]"><br>
<input type="file" name="userfile[]">
</div>
<br><br>
<div>
<input type="text" name="title[]"><br>
<input type="file" name="userfile[]">
</div><br>
<input type="submit" name="" value="enter">
<?php echo form_close(); ?>

如果我尝试将 $files 打印为 print_r($files); 它只是显示 ,

引用链接:Multiple files upload (Array) with CodeIgniter 2.0

最佳答案

$this->upload->do_upload() 仅返回 true 或 false。您必须使用 $name_array[] = $this->upload->data('file_name');

获取数据

关于mysql - 在 codeigniter 中上传后获取文件名作为数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39894358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com