Sure, here's a PHP script that opens a file, checks if it contains a "CSV" folder, and if it does, imports the
CSV files within that folder into a database. This example assumes you are using MySQL as your
database.
Before you run this script, make sure you have the correct database credentials, and that the database
and table you want to import data into already exist.
```php
<?php
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
$directory = "path/to/your/directory";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
// Function to import CSV to database
function importCSVToDatabase($filePath, $conn) {
$tableName = "your_table_name"; // Change to your table name
$file = fopen($filePath, "r");
if ($file === false) {
die("Could not open the CSV file.");
}
// Get the first row which contains the column headers
$columns = fgetcsv($file);
// Prepare the insert query
$query = "INSERT INTO $tableName (" . implode(", ", $columns) . ") VALUES ";
$values = [];
while (($row = fgetcsv($file)) !== false) {
$rowValues = array_map(function ($value) use ($conn) {
return "'" . $conn->real_escape_string($value) . "'";
}, $row);
$values[] = "(" . implode(", ", $rowValues) . ")";
if (count($values) > 0) {
$query .= implode(", ", $values);
if ($conn->query($query) === TRUE) {
echo "New records created successfully\n";
} else {
echo "Error: " . $query . "\n" . $conn->error;
fclose($file);
// Check if the directory contains a CSV folder
$csvDirectory = $directory . "/CSV";
if (is_dir($csvDirectory)) {
// Open the CSV folder
$csvFiles = scandir($csvDirectory);
foreach ($csvFiles as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) == "csv") {
$filePath = $csvDirectory . "/" . $file;
importCSVToDatabase($filePath, $conn);
} else {
echo "CSV folder does not exist in the specified directory.";
$conn->close();
?>
```
### Explanation:
1. **Database Connection**: The script starts by connecting to a MySQL database using `mysqli`.
2. **Import Function**: `importCSVToDatabase` is a function that reads a CSV file and inserts its data
into the specified database table.
3. **Directory Check**: The script checks if a "CSV" directory exists within the specified directory.
4. **CSV File Processing**: If the "CSV" directory exists, the script scans the directory for `.csv` files and
imports each one using the `importCSVToDatabase` function.
5. **Cleanup**: Finally, the script closes the database connection.
### Prerequisites:
- Replace `"your_username"`, `"your_password"`, `"your_database"`, `"path/to/your/directory"`, and
`"your_table_name"` with your actual database credentials, directory path, and table name.
- Ensure the CSV files are correctly formatted and match the database table schema.
This script assumes all CSV files have the same structure and that the table in the database has columns
corresponding to the CSV headers. Adjustments might be necessary depending on your specific use
case.