我在 Codeigniter 的 for 循环中设置上传文件名时遇到问题,我想将上传的文件(歌曲)添加到数据库和某个文件夹。我想将它们命名为 song1、song2、song3... 其中数字是歌曲在数据库中的 ID。这是我的代码:
for ($i = 1; $i < 201; $i++) {
if (isset($_POST['name'.$i])) {
$maxID = $this->Song_model->maxID();
$maxID++;
$config['file_name'] = "song".$maxID;
$config['upload_path'] = "./assets/uploads/";
$config['allowed_types'] = '*';
$config['max_size'] = 0;
$this->load->library('upload', $config);
if ($this->upload->do_upload('file'.$i) == true) {
$data = array(
'IDArt' => $artistID,
'IDAlb' => $newAlbumId,
'songname' => $this->input->post('name'.$i),
'author' => $artistName,
'length' => $this->input->post('length'.$i),
'price' => $this->input->post('price'.$i)
);
$this->Song_model->persist($data);
}
}
}
我上传的许多歌曲中的第一首歌曲获得了好听的名字示例:song11(之前数据库中有 10 首歌曲),但是在这首歌曲之后的歌曲获得了 song111、song 112 的名字。
代码段:
$maxID = $this->Song_model->maxID();
$maxID++;
获取我需要与“歌曲”连接的确切 ID,但似乎我对 $config['file_name'] = "song".$maxID; 这一行
顺便说一句。 song11, song111, song112 当文件上传名称设置为相同的值时发生,在这种情况下为“song11”
最佳答案
我终于找到了解决方案,我用函数 bool move_uploaded_file 修复了它,它可以在上传后动态更改文件名。
关于php - Codeigniter:在for循环中设置上传文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37402205/