Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
1.7 kB
0
Indexable
Never
<?php
session_start();

if (isset($_SESSION['user_id'])) {
    require_once 'database.php';
    $user_id = $_SESSION['user_id'];

    $sql = "SELECT subscription_status, converted_files FROM users WHERE id = :user_id";
    $stmt = $conn->prepare($sql);
    $stmt->bindParam(':user_id', $user_id);
    $stmt->execute();
    $user = $stmt->fetch();

    $sub_status = $user['subscription_status'];
    $converted_files = $user['converted_files'];

    if ($sub_status === 'free' && $converted_files >= 3) {
        echo "Max limit reached, please subscribe";
        exit;
    }
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['csv_file'])) {
    $csvFile = $_FILES['csv_file'];

    if ($csvFile['error'] === UPLOAD_ERR_OK && pathinfo($csvFile['name'], PATHINFO_EXTENSION) === 'csv') {
        $csvData = file_get_contents($csvFile['tmp_name']);

        // Convert CSV data to SQL query as needed
        // Example SQL query generation:
        $sqlQuery = "INSERT INTO users (Name, Email) VALUES ";

        $lines = explode("\n", $csvData);
        foreach ($lines as $line) {
            $fields = str_getcsv($line);
            if (count($fields) === 2) {
                $name = $fields[0];
                $email = $fields[1];
                $sqlQuery .= "('$name', '$email'), ";
            }
        }

        // Remove the last trailing comma and space
        $sqlQuery = rtrim($sqlQuery, ', ');

        // Create a downloadable SQL file
        header('Content-Type: application/sql');
        header('Content-Disposition: attachment; filename=csv_to_sql.sql');
        echo $sqlQuery;
        exit;
    } else {
        echo "Invalid file format. Please upload a CSV file.";
    }
}
}
?>