0

The problem is when I receive email attachments images/binary data becomes corrupted however when I upload files to the same database table they aren't corrupted.

On the left side is the normal image that works (when renamed to PHP and it's binary data is copied). The right side is the image after it's been corrupted. I figured I'd add the image because someone may see it and just know what is going on here. PHP code below...

Left side working image, right side corrupted image.

So I have other files including images stored in the database and they work just fine...

$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
//INSERT INTO table (data), ($data);//etc

However the error is occurring in the PHP code that I use to clean up emails and then store them in the database along with attached files. I know that the attached files are being targeted in PHP correctly so it's simply HOW I'm storing it which seems to be the problem. The data column in MySQL is longblob and uploading images before sending them works fine but when I RECEIVE emails this code that handles putting received attachments is what breaks (using the same database table).

$data = mysqli_real_escape_string($connection,base64_decode($email['attachments'][$t]['attachment']));
$data = mysqli_real_escape_string($connection,$email['attachments'][$t]['attachment']);
$data = base64_decode($email['attachments'][$t]['attachment']);
$data = mysql_real_escape_string(base64_decode($email['attachments'][$t]['attachment']));
$data = addslashes($email['attachments'][$t]['attachment']);
John
  • 1
  • 13
  • 98
  • 177
  • Why store the files in the database in the first place? – Mike Brant Mar 30 '14 at 06:15
  • If it's *your* attachment and not mine PHP stands between me and your file, Apache is an all-or-nothing deal, I take privacy and security seriously. – John Mar 30 '14 at 06:17
  • are you sure the email attachments are not corrupted in the first place? – klarki Mar 30 '14 at 11:15
  • @John If security is your driving design concern, it seems like you should be focusing on encrypting the files at rest rather than simply storing in DB, where someone with DB permissions could access the file just as easily as if it were in a file system directory somewhere. – Mike Brant Mar 30 '14 at 15:25
  • Files aren't corrupt because I send them **FROM** multiple third parties **TO** multiple emails including my own. If I encrypt the files you can still GET them and then later figure out how to decrypt them. – John Mar 30 '14 at 21:07

0 Answers0