2

I wanna save mail attachments with size in database.

So I open mail in text mode by php,for attachments I can see something like example:

Content-Type: image/jpeg; name="donoghte D2.jpg"
Content-Disposition: attachment; filename="donoghte D2.jpg"
Content-Transfer-Encoding: base64
X-Attachment-Id: f_gvn2345e0
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcG
BwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwM ...

I will show it by this code

<?php
header('Content-Type: image/jpeg');
echo (base64_decode($text));
?>

If I wanna to calculate the size of this file,and store it and its size in database,what is the best way?

  1. Should I save encode64 of this(like what sent in mail) in a database?
  2. If so, what should the datatype of that field be?
  3. To calculate size of it, should I decode it, then get strlen of it? or is there any faster way?

with special thanks for your attention

Dennis C
  • 13
  • 5
Moein Hosseini
  • 4,309
  • 15
  • 68
  • 106

4 Answers4

0

to get a string length in PHP you can use strlen()

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

You're dealing with a binary object there, so it's probably best to store it as the same. I'm not sure what database you're using, but MySQL has the BLOB (Binary Large OBject) for this exact purpose.

You could also write it to the file system. There's dozens of good discussions about the merits of both techniques on Stack Overflow, so I won't go into it here (eg: Storing images in DB? Yea or Nay?)

I believe that if you have the decoded data in a string, then strlen would give you the file size of it. You could also query the database or filesystem after storage to get it.

Community
  • 1
  • 1
nickf
  • 537,072
  • 198
  • 649
  • 721
0

format I would use blob which enables you to store binary data in base64_decoded form. But if you don't care about storage capacity and want to resend the attachment, you may store the base64_encoded data (to save processing time) in any text format the DB supports. If you care about DB storage capacity, I would save the file separatedly and store only file name and path into DB.

get file size To get the image length, use strlen on decoded data. It would be better to use also Content-length header.

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
-1

According to http://us3.php.net/manual/en/function.mb-strlen.php#47309, the following code should give you the string length with multibyte characters counted as 2 characters.

mb_strlen($utf8_string, 'latin1');

However, I would suggest saving the file on disk, as this is usally a lot better performance wise, pro's and con's listed in nickf's post: https://stackoverflow.com/a/8339065/863577

Community
  • 1
  • 1
Dennis C
  • 13
  • 5