0

I'm using SwiftMailer and trying to add an attachment with special chars and spaces in it's filename. How can I add such a file, f.e. "Informationen für alle Mitglieder.pdf"?

I use this command:

$message->attach("resources/assets/docs/Informationen für alle Mitglieder.pdf");

...but I'm getting always an file not found error. When I replace all special chars, it works fine?

Is there any "decoding function" to make it work? For white spaces, I can replace them easily with "\x20" and it works. But what's about the other chars like "äöü"?

user2891491
  • 351
  • 8
  • 16
  • don't use filenames with whitespaces it was and will never be a good solution – donald123 Jul 02 '15 at 12:46
  • White spaces are not the problem, I can replace them with \x20 - but what's about "äöü". We have 2015, it's terrible that this is still a problem. – user2891491 Jul 02 '15 at 12:54

2 Answers2

2
$message->attach("resources/assets/docs/Informationen_fuer_alle_Mitglieder.pdf");

Just rename your .pdf. And don't take special characters and white spaces in your urls in future.

Maybe this is useful for you:

$string = "resources/assets/docs/Informationen für alle Mitglieder.pdf"
$string  = str_replace(' ', '%20', $string); // %20 means white space
$string  = str_replace('ü', '&uuml', $string); // %uuml means ü
echo $string;

EDIT2:

http://www.webspace-finden.de/html-php-ae-oe-ue-und-szett-zeichencodes/

Ä – & Auml;

ä – & auml;

Ö – & Ouml;

ö – & ouml;

Ü – & Uuml;

ü – & uuml;

remove white space behind "&" otherwise I couldn't show this

http://php.net/manual/de/function.urlencode.php

<?php
echo urlencode($string); >;
?>

EDIT3:

$string = "resources/assets/docs/Informationen%20f&uumlr%20alle%20Mitglieder.pdf"
  • Nope, I need the special chars. – user2891491 Jul 02 '15 at 12:53
  • remembered something I've added in my answer –  Jul 02 '15 at 12:55
  • Yeah, that is for urls. For internal filenames I've to add "\x20" to replace white spaces. I've updated my question, because I've still problems with "äüö". – user2891491 Jul 02 '15 at 12:56
  • Hey Maurize, I'm sorry to say that it isn't working. urlencode() works for HTTP urls, "ä" and the others are for HTML. I need to encode that path to work with fopen("resources/assets/docs/Informationen für alle Mitglieder.pdf","r"); – user2891491 Jul 02 '15 at 14:06
  • then this will help you: http://stackoverflow.com/questions/2216852/php-file-handling-special-characters-in-folder-names –  Jul 02 '15 at 14:12
  • http://stackoverflow.com/questions/1525830/how-do-i-use-filesystem-functions-in-php-using-utf-8-strings btw im german. –  Jul 02 '15 at 14:43
  • 1
    Why are you trying to escape for html though? I don't think this problem is anything to do with html – Phil Jul 03 '15 at 07:46
1

Have you tried using the attach method correctly as per the documentation?

$message->attach(Swift_Attachment::fromPath('resources/assets/docs/Informationen für alle Mitglieder.pdf'));

Also, there is nothing wrong with spaces or extended ascii in filenames. There are not many operating systems which dont support it.

Phil
  • 1,996
  • 1
  • 19
  • 26