Hi all,

I'd like to know if it is possible to create a php script that would take the names of image files with its extension of say 10,000 images from out of a folder and place them into a database table in separate rows to use as sample data?

Thank you

Dani AI

Generated

As asked: yes — importing 10,000 image filenames into a database from PHP is routine. was right that a filesystem read plus inserts will do the job; below are practical, production-minded alternatives and pitfalls to watch for.

A reliable workflow: enumerate files with PHP's SPL iterator classes (for recursion and low memory use) and extract sanitized names with pathinfo. Consider computing a checksum (for example with hash_file) to detect duplicates before inserting. For large batches, write out a CSV and use MySQL's bulk loader, or insert in transactions with parameterized statements in chunks to avoid memory/timeouts.

Schema and insert tips: store metadata (for example id, filename, path, extension, size, checksum, created_at) and put a UNIQUE index on the column(s) that must be unique. Use prepared statements and transactions (PDO is a common choice) and commit every few hundred rows for a good balance of speed and safety. If you need maximum import speed, export a CSV and use LOAD DATA INFILE server-side — it is much faster than issuing 10k individual INSERTs.

Common gotchas: filename character encoding (use utf8mb4 on the DB side), OS path separators, files with special characters, and file permissions. Avoid building SQL by concatenating filenames (use bound parameters) and log any rows that fail so you can retry. For reference: PHP SPL iterators, PDO prepared statements/transactions, and MySQL LOAD DATA INFILE are good starting points (RecursiveDirectoryIterator, PDO prepared statements, LOAD DATA INFILE).

Recommended Answers

All 2 Replies

Sure. You can use glob to read the files, and create an insert loop, or sql file with the queries.

Thanks for ypur help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.