How to store directly in MySQL

xiaoxiao2021-03-06  123

Author: Florian Dittmer translation: netmad

If you want to save binary data, such as pictorial files and html files, save directly in your MySQL database, then this article is written for you!

I will tell you how to store these files through the HTML form, how to access and use these files.

This article overview:

Create a new database in MySQL

An example of how to store files

An example of how to access a file

Create a new Database in MySQL

First, you must create a new database in your MySQL, we will store those binary files in this database. In the example, I will use the following structure. In order to establish a database, you must do the following steps:

Enter the MySQL controller

Enter the command "CREATE DATABASE BINARY_DATA;"

Enter the command "use binary_data;"

Enter the command "create table binary_data (id int (4) Not null auto_increment primary key,

Description Char (50), Bin_Data Longblob, FileName Char (50), FileSize Char (50), FileType Char (50)); "(Can't break)

If there is no accident, the database and table should be established.

An example of how to store files With this example you can transfer files into the database via HTML form. .

STORE.PHP3

// store.php3 - by florian dittmer

?>

Store Binary Data INTO SQL Database </ Title> </ head></p> <p><Body></p> <p><? PHP</p> <p>// If the form is submitted, the code will be executed:</p> <p>IF ($ SUBMIT) {</p> <p>/ / Connect to the database</p> <p>// (You may need to adjust the host name, username and password)</p> <p>MySQL_Connect ("LocalHost", "root", "password");</p> <p>MySQL_SELECT_DB ("binary_data");</p> <p>$ DATA = AddSlashes (Fread (FOPEN ($ Form_Data, "R"), FileSize ($ form_data)));</p> <p>$ Result = mysql_query ("INSERT INTO BINARY_DATA (Description, Bin_Data, FileName, FileSize, FileType".</p> <p>"VALUES ('$ form_description', '$ data', '$ form_data_name', '$ form_data_size', '$ form_data_type')");</p> <p>$ ID = mysql_insert_id ();</p> <p>PRINT "<p> this file has the folload database ID: <b> $ ID </ b>";</p> <p>MySQL_Close ();</p> <p>} else {</p> <p>/ / Otherwise, the form of the store is stored new data</p> <p>?></p> <p><form method = "POST" action = "<? PHP Echo $ PHP_SELF;?>" EncType = "Multipart / Form-Data"></p> <p>FILE DESCRIPTION: <br></p> <p><Input Type = "text" name = "form_description" size = "40"></p> <p><Input Type = "Hidden" name = "max_file_size" value = "1000000"></p> <p><br> File to Upload / Store In Database: <br></p> <p><Input Type = "file" name = "form_data" size = "40"></p> <p><p> <input type = "submit" name = "submit" value = "Submit"></p> <p></ form></p> <p><? PHP</p> <p>}</p> <p>?></p> <p></ Body></p> <p></ Html></p> <p>If you perform this program, you will see a simple HTML form, click "Browse" to select a file, and then click Submit.</p> <p>After the file is uploaded to the web server, the program will tell you the ID of the file just uploaded, remember this ID, and will be used.</p> <p>An example of how to access a file</p> <p>You can access the file you just stored through this program.</p> <p><? PHP</p> <p>// getdata.php3 - by florian dittmer <dittmer@gmx.net></p> <p>// Call method: getData.php3? Id = <id></p> <p>IF ($ ID) {</p> <p>/ / You may need to adjust the host name, username, and password:</p> <p>@MYSQL_Connect ("LocalHost", "root", "password");</p> <p>@MYSQL_SELECT_DB ("binary_data");</p> <p>$ query = "SELECT BIN_DATA, FILETYPE from Binary_Data where id = $ ID";</p> <p>$ Result = @Mysql_Query ($ query);</p> <p>$ DATA = @Mysql_Result ($ Result, 0, "Bin_Data");</p> <p>$ TYPE = @Mysql_Result ($ Result, 0, "FileType");</p> <p>Header ("Content-Type: $ TYPE");</p> <p>Echo $ DATA;</p> <p>}</p> <p>?></p> <p>The program must know to access that file, you must use the ID as a parameter.</p> <p>For example: a file in a database is 2. You can call it like this:</p> <p>GetData.php3? id = 2 If you store the picture in the database, you can call it as calling the image.</p> <p>EXAMPLE: A image file in the database is 3. You can call it like this:</p> <p><img src = "getData.php3? id = 3"></p> <p>How to store more than 1MB files:</p> <p>If you want to store more than 1MB files, you must make a lot of modifications to your program, PHP settings, and SQL settings.</p> <p>Here are a few of you may help you save less than 24MB files:</p> <p>Modify Store.php3, change the value of max_file_size to 24000000.</p> <p>Modify your PHP settings, in general, PHP only allows less than 2MB files, you must change max_filesize to the value of 24000000 to remove MySQL's packet size, in general mysql less than 1 MB packet.</p> <p>You must restart your mysql with the following parameters.</p> <p>/ usr / local / bin / safe_mysqld -o key_buffer = 16m -o table_cache = 128 -o sort_buffer = 4m -o record_buffer = 1m -o max_allowed_packet = 24m</p> <p>If still an error:</p> <p>May be timeout errors, if you store a big file through a very slow connection, PHP default time limit is 30 seconds.</p> <p>You can change the value of max_execution_time to -1</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-102427.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="102427" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.053</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '6Dwc08_2FUf1l6rajzlEql4_2BN9qdOar2Y8kQ2wsivtktACqGEc8BuOm9pQTV8LN8iWn7Un_2FxdYI2jWbdg6FEEgFw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>