从HTML表单将文件存储在MySQL数据库中 [英] Store files in MySQL database from HTML form

查看:233
本文介绍了从HTML表单将文件存储在MySQL数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下HTML表单。您可以看到它正在保存一个文件。

 < form enctype =multipart / form-dataaction =welcome。 phpmethod =post 
onsubmit =return validateFormOnSubmit(this)>
< b>名称< / b>< br /> < input type =textname =fname/>
< br />
< b>说明< / b>< br /> < TEXTAREA NAME =descriptionCOLS = 40
ROWS = 6>< / TEXTAREA>< br />< br /> < input type =hiddenname =MAX_FILE_SIZEvalue =100000/>

< b>上传图片< / b>< br /> < input name =uploadedfiletype =file/>< br />
< b>< b> Latitude< / b>< br />>< br / gt>< INPUT name =latid =lat
< b>经度< / b>< br />< INPUT name =lngid =lng/> < input type =submit/>



正在处理此文件所以...

  $ target_path =uploads /; 

$ target_path = $ target_path。 basename($ _FILES ['uploadedfile'] ['name']);

其余信息存储在SQL数据库中。我想存储上传到该数据库的文件。我将如何通过php文件发送信息来执行此操作?

  $ query =INSERT INTO< TABLE>(name,message,lat,lng ,type)VALUES('。$ title。','。。$ des。','。。$ lati。','。。$ long。','。。$ type。') ; 
$ q = mysql_query($ query)或者死(mysql_error());


解决方案

如果您一定要将HTML文件插入到您的MySQL数据库有两种方法:使用BLOB类型来存储文件。 使用BLOB类型来存储文件。

  • 将文件转换为字符串,因为它是HTML,并将其存储在TEXT字段中。


  • 然而,我的建议是将文件存储在服务器文件系统中,而不是数据库中。


    I've got the following HTML form. As you can see it's saving a file.

    <form enctype="multipart/form-data" action="welcome.php" method="post
    "onsubmit="return validateFormOnSubmit(this)"> 
     <b>Name</b><br/> <input type="text" name="fname" /> 
     <br/>
     <b>Description</b><br/> <TEXTAREA NAME="description" COLS=40
     ROWS=6></TEXTAREA><br/><br/> <input type="hidden" name="MAX_FILE_SIZE" value="100000"/> 
    
    <b>Upload Picture</b><br/> <input name="uploadedfile" type="file" /><br /> 
     <b>Latitude</b><br/><INPUT name="lat" id ="lat" /><br/> 
     <b>Longitude</b><br/><INPUT     name="lng" id ="lng" /><br/> <input type="submit" />
    

    This file is being processed like so...

     $target_path = "uploads/";
    
     $target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
    

    The rest of the information is being stored in a SQL database. I'd like to store the file uploaded in that database. How would I go about doing this via the php file the information get's sent to? I'm inserting it like..

       $query = "INSERT INTO <TABLE> (name, message, lat, lng, type)VALUES ('".$title."', '".$des."', '".$lati."', '".$long."', '".$type."')";
       $q=mysql_query($query) or die(mysql_error());
    

    解决方案

    If you definitely want to insert your HTML file in your MySQL DB, there are two ways:

    1. Use the BLOB type to store the file.

    2. Convert your file into a string, as it is HTML, and store it in a TEXT field.

    However, my recommendation would be to store files on the server filesystem, and not in the database.

    这篇关于从HTML表单将文件存储在MySQL数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆