-
Notifications
You must be signed in to change notification settings - Fork 83
Fix attachment filename generation for URLs with query parameters #2499
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Strip query parameters from URLs before generating filenames to prevent 'Filename too long' errors. This is particularly important for Instagram and other CDN URLs that include long query strings with cache-busting parameters. Without this fix, URLs like: https://example.com/image.jpg?stp=dst-jpg_e35&nc_cat=101&... Would generate filenames like: image.jpgstp=dst-jpg_e35_nc_cat=101_... (400+ characters) Now correctly generates: image.jpg Fixes in both save_attachment() and save_file() methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes filesystem errors caused by excessively long filenames when downloading attachments from URLs containing query parameters. The fix strips query strings before generating filenames, preventing "Filename too long" errors commonly encountered with Instagram CDN URLs and similar services.
Key Changes:
- Modified filename generation to strip query parameters using
strtok()before extracting the basename - Applied the fix to both
save_attachment()andsave_file()methods
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| includes/class-attachments.php | Strips query parameters from URLs before generating attachment filenames in both save methods |
| .github/changelog/2499-from-description | Adds changelog entry documenting the bug fix |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Tests verify that: - save_attachment() strips query parameters from attachment URLs - save_file() strips query parameters from direct file storage URLs - Filenames are clean and don't contain ? or query string components This ensures the fix for 'Filename too long' errors is properly tested.
- Remove unused variable to fix phpcs warning - Use assertStringEndsWith instead of assertStringContainsString for file extension checks
Changed from strtok() to wp_parse_url() to properly handle URL fragments (anchors with #) in addition to query parameters. This matches WordPress core's approach in download_url() and ensures filenames never contain fragment identifiers.
Proposed changes:
Other information:
Testing instructions:
Before:
After:
File saved successfully as
582455906_17930138757114605_3007218277985087381_n.jpgChangelog entry
Changelog Entry Details
Significance
Type
Message
Fix "Filename too long" errors when downloading attachments from URLs with query parameters (e.g., Instagram CDN URLs).