How can PHP be used to automatically index images from an FTP folder into a MySQL database and generate thumbnails?

To automatically index images from an FTP folder into a MySQL database and generate thumbnails, you can use PHP to connect to the FTP server, retrieve the list of images, store the image details in the database, and generate thumbnails using a library like GD or Imagick.

<?php
// Connect to FTP server
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$ftp_folder = '/images/';

$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);

// Get list of files in FTP folder
$files = ftp_nlist($conn_id, $ftp_folder);

// Connect to MySQL database
$servername = 'localhost';
$username = 'db_username';
$password = 'db_password';
$dbname = 'db_name';

$conn = new mysqli($servername, $username, $password, $dbname);

// Insert image details into database and generate thumbnails
foreach ($files as $file) {
    $image_path = 'ftp://' . $ftp_user . ':' . $ftp_pass . '@' . $ftp_server . $file;

    // Insert image details into database
    $sql = "INSERT INTO images (image_path) VALUES ('$image_path')";
    $conn->query($sql);

    // Generate thumbnail
    $image = imagecreatefromstring(file_get_contents($image_path));
    $thumb = imagecreatetruecolor(100, 100);
    imagecopyresized($thumb, $image, 0, 0, 0, 0, 100, 100, imagesx($image), imagesy($image));

    // Save thumbnail
    imagejpeg($thumb, 'thumbnails/' . basename($file));
}

// Close FTP connection and MySQL connection
ftp_close($conn_id);
$conn->close();
?>