Codeigniter:在for循环中设置上传文件名
Codeigniter: Set upload file name in for loop
我在 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);
}
}
}
我上传的许多歌曲中的第一首歌曲获得了好听的名字示例:歌曲 11(之前数据库中有 10 首歌曲),但是在这首之后的歌曲获得了歌曲 111、歌曲 112 的名称。
代码段:
$maxID = $this->Song_model->maxID();
$maxID++;
获取我需要与 "song" 连接的确切 ID,但是,$config['file_name'] = "song" 似乎我错了。 $maxID; 这一行
顺便说一句。 song11, song111, song112 在这种情况下文件上传名称设置为相同的值时发生 "song11"
我终于找到了解决方案,我用函数 bool move_uploaded_file 修复了它,它可以在上传后动态更改文件名。
我在 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);
}
}
}
我上传的许多歌曲中的第一首歌曲获得了好听的名字示例:歌曲 11(之前数据库中有 10 首歌曲),但是在这首之后的歌曲获得了歌曲 111、歌曲 112 的名称。
代码段:
$maxID = $this->Song_model->maxID();
$maxID++;
获取我需要与 "song" 连接的确切 ID,但是,$config['file_name'] = "song" 似乎我错了。 $maxID; 这一行
顺便说一句。 song11, song111, song112 在这种情况下文件上传名称设置为相同的值时发生 "song11"
我终于找到了解决方案,我用函数 bool move_uploaded_file 修复了它,它可以在上传后动态更改文件名。