Bulk Import - Company Logos

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harrytruman
    Member
    • Jun 2023
    • 98

    Bulk Import - Company Logos

    Hi Everyone, I'd like to perform a bulk upload of company profiles with company logos. When I export a record into Excel, it includes the logo image in the "logo" column. However, when I convert this to a CSV for import, the logo image file disappears (not supported with CSV files).

    Is there a solution or any ideas for doing this? Right now, I would have to upload a logo image for each individual company record. There are hundreds in my project so that's not going to work.

    Thanks!
  • shalmaxb
    Senior Member
    • Mar 2015
    • 1592

    #2
    This has been a question already for years and appears a few times here in the forum.

    I do not have the skills to code something, that would achieve this, but I played a bit with AI and this is one result so far:

    PHP Code:
    <?php
    $targetDir = "uploads/"; // Directory where images will be uploaded
    $images = scandir($targetDir); // Get all files in the directory
    
    $servername = "your_server";
    $username = "your_username";
    $password = "your_password";
    $dbname = "your_database";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    
    $imageIndex = 1; // Initialize image index
    
    foreach ($images as $image) {
        if ($image != "." && $image != "..") {
            $uniqueId = uniqid(); // Generate a unique ID for each image
            $imagePath = $targetDir . $image;
    
            // Update the database with the image reference using the existing record ID
            $sql = "UPDATE your_table SET image_id = '$uniqueId', image_path = '$imagePath' WHERE id = $imageIndex";
            if ($conn->query($sql) === TRUE) {
                echo "Image $image uploaded successfully with ID $uniqueId and mapped to record $imageIndex.<br>";
            } else {
                echo "Error: " . $sql . "<br>" . $conn->error;
            }
    
            $imageIndex++; // Increment image index for the next record
        }
    }
    
    $conn->close();
    ?>

    Explanation
    1. Initialize Image Index: Start with an image index of 1.
    2. Loop Through Images: For each image in the directory, generate a unique ID and construct the image path.
    3. Update Database: Use an UPDATE SQL statement to set the image_id and image_path for the corresponding data record based on the image index.
    4. Increment Image Index: After processing each image, increment the image index to map the next image to the next data record.

    This script ensures that each image is mapped to a specific data record in sequence. Adjust the table and column names as needed to fit your database schema. Let me know if you need any more help with this!

    I do not know, if this would really work and it will need perhaps more features. But it could be a start.

    Comment

    Working...