在php中获取文件的大小
我想将邮件附件的大小保存在数据库中。
因此,我通过 php 以文本模式打开邮件,对于附件,我可以看到类似示例的内容:
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 ...
我将通过此代码显示它
<?php
header('Content-Type: image/jpeg');
echo (base64_decode($text));
?>
如果我想计算该文件的大小,并将其及其大小存储在数据库中,什么是最好的方式?
- 我应该将其encode64(例如通过邮件发送的内容)保存在数据库中吗?
- 如果是这样,该字段的数据类型应该是什么?
- 要计算它的大小,我应该解码它,然后获取它的
strlen
吗?或者有什么更快的方法吗?
特别感谢您的关注
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
strlen
of it? or is there any faster way?
with special thanks for your attention
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您正在处理一个二进制对象,因此最好将其存储为相同的对象。我不确定你使用的是什么数据库,但 MySQL 有
BLOB
(二进制大对象)就是为了这个目的。您也可以将其写入文件系统。 Stack Overflow 上有很多关于这两种技术优点的很好的讨论,所以我不会在这里深入讨论(例如:在数据库中存储图像?是或否?)
我相信,如果您将解码数据存储在字符串中,那么
strlen 会给你它的文件大小。您还可以在存储后查询数据库或文件系统来获取它。
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.要在 PHP 中获取字符串长度,可以使用 strlen()
to get a string length in PHP you can use strlen()
格式我会使用blob使您能够以 base64_decoded 形式存储二进制数据。但是,如果您不关心存储容量并想要重新发送附件,则可以以数据库支持的任何文本格式存储 base64_encoded 数据(以节省处理时间)。如果您关心数据库存储容量,我会单独保存文件,仅将文件名和路径存储到数据库中。
获取文件大小 要获取图像长度,请对解码数据使用 strlen。最好同时使用
Content-length
标头。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.根据 https://www.php.net/manual/ en/function.mb-strlen.php#47309,以下代码应为您提供字符串长度,其中多字节字符计为 2 个字符。
mb_strlen($utf8_string, 'latin1');
但是,我建议将文件保存在磁盘上,因为这通常会带来更好的性能,nickf 的帖子中列出了优点和缺点:https://stackoverflow.com/a/8339065/863577
According to https://www.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