Using your code, with a unique index on the name column, only takes 7 seconds for 300 rows inserted into an empty table for me. What else is the code on the page doing?
Here are some application issues and improvements for the code -
- The function should be general-purpose and accept a relative directory/path and the database connection as call-time parameters.
- The function name should not end in a number, indicating you are probably going to copy/paste the definition a number of times, with a different hard-coded directory/path value in each one.
- You should only store a minimum amount of data in the database table. The full server directory/path is application specific and should not be stored in the database (if you move the application to a different server/path, the dir column data will be wrong.) You should only store the filename and the relative path in the database table.
- You should prepare and bind the inputs only once, before the start of the looping. This will result in a small performance gain.
- The
$filename;
variable on a line by itself does nothing and should be removed. - You can achieve a small performance gain by just using glob() to get a list of .mp3 files in the directory, then loop over those matching files. No need for the pathinfo() and the if() conditional logic.