Before anything else, you need to add an ORDER BY clause to your query so that you can ensure a consistent order of your results. Without an ORDER BY clause, the database server is allowed to return the rows in whatever order it wants, usually that's just whatever order it finds them in as it scans the indexes/tables. As you add/remove data that order will change.
Once you have your results ordered in a specific way, you can count the number of records that come before a specific row by running a query with a where condition of theOrderByColumn <= yourCurrentRowsOrderByColumn. For example.
SELECT id,path, subCategoryId
FROM `files`
WHERE categoryId=5 AND subCategoryId=15
ORDER BY id;
SELECT COUNT(*)
FROM files
WHERE categoryId=5 AND subCategoryId=15 AND id <= 307