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?
- Should I save encode64 of this(like what sent in mail) in a database?
- If so, what should the datatype of that field be?
- To calculate size of it, should I decode it, then get
strlenof it? or is there any faster way?
with special thanks for your attention
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
strlenwould give you the file size of it. You could also query the database or filesystem after storage to get it.