You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PrestaShop 1.7:如何通过CSV导入/更新商品附件(无需本地上传)

Solution for Importing PrestaShop Product Attachments from Server-Side Files via CSV

Since your 2GB files are already hosted on the server, you don’t need to waste bandwidth re-uploading them. Here are two reliable, server-side-focused approaches to link these files to your products using your CSV:

Approach 1: Custom PHP Script (Most Direct & Flexible)

This method uses PrestaShop’s native core classes to safely read your CSV, verify server-side file existence, and attach files to products—no risky direct database edits required.

Step 1: Prepare Your CSV

Structure your CSV with two columns (add a header row for clarity):

  • product_id: The numeric ID of your PrestaShop product
  • attachment_filepath: Full server path to the file (e.g., /var/www/html/your-store/assets/large-product-manual.pdf)

Save it as product_attachments.csv in your PrestaShop root directory.

Step 2: Create the Import Script

Make a file named import_attachments.php in your PrestaShop root, then paste this code:

<?php
require_once(dirname(__FILE__).'/config/config.inc.php');
require_once(dirname(__FILE__).'/init.php');

// Path to your CSV file
$csvPath = 'product_attachments.csv';

// Validate CSV exists
if (!file_exists($csvPath)) {
    die("Error: CSV file not found at $csvPath");
}

// Open CSV for reading
$csvHandle = fopen($csvPath, 'r');
if (!$csvHandle) {
    die("Error: Could not open CSV file for reading");
}

// Skip header row (remove this line if your CSV has no header)
fgetcsv($csvHandle);

// Process each row
while (($row = fgetcsv($csvHandle, 1000, ',')) !== FALSE) {
    $productId = (int)$row[0];
    $filePath = trim($row[1]);

    // Check if product exists
    $product = new Product($productId);
    if (!Validate::isLoadedObject($product)) {
        echo "Skipping: Product ID $productId not found\n";
        continue;
    }

    // Check if file exists on server
    if (!file_exists($filePath) || !is_readable($filePath)) {
        echo "Skipping: File $filePath is missing or unreadable for product $productId\n";
        continue;
    }

    // Extract file details
    $fileName = basename($filePath);
    $fileSize = filesize($filePath);
    $fileMime = mime_content_type($filePath);
    $defaultLangId = (int)Configuration::get('PS_LANG_DEFAULT');

    // Create attachment object
    $attachment = new Attachment();
    $attachment->name = array($defaultLangId => $fileName);
    $attachment->file = $fileName;
    $attachment->mime = $fileMime;
    $attachment->file_size = $fileSize;
    $attachment->description = array($defaultLangId => ''); // Add a description here if needed
    $attachment->add();

    // Link attachment to product if creation succeeded
    if ($attachment->id) {
        Db::getInstance()->insert('product_attachment', [
            'id_product' => $productId,
            'id_attachment' => $attachment->id,
            'file' => $fileName
        ]);
        echo "Success: Attached $fileName to product $productId\n";
    } else {
        echo "Failed: Could not create attachment for $fileName (product $productId)\n";
    }
}

fclose($csvHandle);
echo "\nImport process completed!";
?>

Step 3: Run the Script

  1. Upload both the CSV and script to your PrestaShop root directory.
  2. Ensure the web server user (e.g., www-data) has read access to your attachment files.
  3. Run the script via SSH (php import_attachments.php) or your browser (visit https://your-store-url.com/import_attachments.php).
  4. Critical: Delete the script and CSV after use to avoid security risks.

Approach 2: Customized Module (For Non-Technical Users)

If you prefer a GUI solution, you can adapt an existing import module to support server-side paths:

  • Use a module like Advanced Import/Export (most default modules don’t support this out of the box).
  • Add a custom hook to the module’s import logic that checks for server file paths instead of requiring local uploads.
  • Alternatively, hire a PrestaShop developer to modify the module to fit your needs.

Key Tips

  • Server Limits: For large batches, adjust your php.ini settings to increase max_execution_time and memory_limit (no upload limits needed since files are already on the server).
  • Testing: Always run this on a staging environment first to avoid breaking your live store.
  • Permissions: Double-check that your web server can read the attachment files (use chmod or your hosting control panel to set proper permissions).

内容的提问来源于stack exchange,提问作者Nasouh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:46:02