How to store .txt files MySQL database?

会有一股神秘感。 提交于 2019-12-30 04:54:25

问题


Can I store data files (e.g. txt files) to the MySql server? If I can, how to store them?


回答1:


You can use LOAD DATA INFILE to read the contents of a file and store it in a table in the database in a structured format.

This can be considerably faster than reading and parsing the file on the client and then using multiple INSERT statements.

Example:

LOAD DATA INFILE 'data.txt' INTO TABLE db2.my_table;



回答2:


Yes you can, but you would probably be better off storing them on the file system and storing a path to the file in the DB.

There are a few SO Posts that discuss this:

  • Storing Images in DB - Yea or Nay?
  • Storing a file in a database as opposed to the file system?



回答3:


Sure you can. I would suggest reading the data from your files and then saving it in your database, i would say in a text field.

You can use something like this to get the file's content:

$file = file_get_contents('./yourfile.txt');

Then insert it

Insert into myTable VALUES (mytextfile = $file)



回答4:


How much text are we talking about here? If there's not that much text, you can store just the content of the file in the database.

You can also store the file itself as a BLOB. http://dev.mysql.com/doc/refman/5.5/en/blob.html

If you'll be dealing with many files, you'll probably be better off storing the files on your sever with the file path in the database.



来源:https://stackoverflow.com/questions/5278759/how-to-store-txt-files-mysql-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!