PrestaShop 1.7:如何通过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 productattachment_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
- Upload both the CSV and script to your PrestaShop root directory.
- Ensure the web server user (e.g.,
www-data) has read access to your attachment files. - Run the script via SSH (
php import_attachments.php) or your browser (visithttps://your-store-url.com/import_attachments.php). - 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.inisettings to increasemax_execution_timeandmemory_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
chmodor your hosting control panel to set proper permissions).
内容的提问来源于stack exchange,提问作者Nasouh

