1

I'm having problem with setting upload file names in for loop in Codeigniter, I want to add uploaded files (songs) to database and to some folder. I want to name them as song1, song2, song3... where number is song's ID in database. Here's my code:

   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);
            }


        }

    }

First song of many that i'm uploading get good name example: song11 (10 songs were in database before), but songs that are coming after this one, gets names song111, song 112.

Code segment:
$maxID = $this->Song_model->maxID();
$maxID++;

Gets me exact ID that i need to concatenate with "song" but, it seems I'm wrong with $config['file_name'] = "song".$maxID; this line

Btw. song11, song111, song112 happens when file upload name is set to same value in this case "song11"

2
  • Why are you using 'file'.$i when what you want I think you want is $config['file_name']? Commented May 24, 2016 at 0:21
  • @thomasw_lrd 'file'.$i is location of input type="file" on my client side, i have more than one file fields so i put 'file' + integer to hit exact field? Commented May 24, 2016 at 0:48

1 Answer 1

1

I've finally found out solution, I've fixed it with function bool move_uploaded_file which can dynamically change file name after upload.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.