How to insert a file in MySQL database?

后端 未结 4 929
青春惊慌失措
青春惊慌失措 2020-11-29 05:18

I want to insert a file in MYSQL database residing on a remote webserver using a webservice.

My question is: What type of table column (e.g. varchar, etc.) will stor

相关标签:
4条回答
  • 2020-11-29 05:35

    File size by MySQL type:

    • TINYBLOB 255 bytes = 0.000255 Mb
    • BLOB 65535 bytes = 0.0655 Mb
    • MEDIUMBLOB 16777215 bytes = 16.78 Mb
    • LONGBLOB 4294967295 bytes = 4294.97 Mb = 4.295 Gb
    0 讨论(0)
  • 2020-11-29 05:40

    The BLOB datatype is best for storing files.

    • See: How to store .pdf files into MySQL as BLOBs using PHP?
    • The MySQL BLOB reference manual has some interesting comments
    0 讨论(0)
  • 2020-11-29 05:46

    The other answers will give you a good idea how to accomplish what you have asked for....

    However

    There are not many cases where this is a good idea. It is usually better to store only the filename in the database and the file on the file system.

    That way your database is much smaller, can be transported around easier and more importantly is quicker to backup / restore.

    0 讨论(0)
  • 2020-11-29 05:52

    You need to use BLOB, there's TINY, MEDIUM, LONG, and just BLOB, as with other types, choose one according to your size needs.

    TINYBLOB 255
    BLOB 65535
    MEDIUMBLOB 16777215
    LONGBLOB 4294967295
    (in bytes)
    

    The insert statement would be fairly normal. You need to read the file using fread and then addslashes to it.

    0 讨论(0)
提交回复
热议问题