How can dynamic filenames be generated using data from a database in PHP?
To generate dynamic filenames using data from a database in PHP, you can query the database to retrieve the necessary data and then use that data to construct the filename. This can be achieved by concatenating the data fields or using a unique identifier as part of the filename. By dynamically generating filenames based on database data, you can ensure that each file is uniquely named and easily identifiable.
// Assuming you have a database connection established
// Query the database to retrieve necessary data
$query = "SELECT id, name FROM products WHERE id = 123";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
// Generate dynamic filename using data from the database
$filename = $row['id'] . '_' . $row['name'] . '.txt';
// Use the generated filename for file operations
file_put_contents($filename, 'File content here');