php+mysql存取文件

近期的项目由于特殊原因,需要将文件存到数据库中。今天特地测试,首先在php网站上传文件,将文件读取出来——再存入到MySQL数据库中。

一、首先创建php 代码如下(网上找了段代码进行过修改):源代码 http://blog.csdn.net/jonathanlin2008/article/details/6185162

<?php

/**

 * Created by PhpStorm.

 * User: yun

 * Date: 2015/7/10

 * Time: 22:04

 */

//echo md5('ggHScquI8EzIPSwV');



if($_SERVER['REQUEST_METHOD'] == 'POST'){

    $myfile=$_FILES['myfile'];

   /* echo file_get_contents($myfile['tmp_name']);

    exit;*/

if($myfile != "none" && $myfile != "") { //有了上传文件了



    //设置超时限制时间,缺省时间为 30秒,设置为0时为不限时

    $time_limit=60;

    set_time_limit($time_limit); //



    //名字,大小,文件格式,

    $file_name=$myfile['name'];

    $file_size=$myfile['size'];

    $file_type=strstr ( $file_name, '.' );



    //把文件内容读到字符串中  直接在读取的临时文件,因为不需要将文件放到目录中。

    $fp=fopen($myfile['tmp_name'],  "rb");

    if(!$fp) die("file open error");

    $file_data = addslashes(fread($fp, $file_size));

    fclose($fp);





   /* $dbh = new PDO ( 'mysql:host=192.168.1.168;port=3306;dbname=testdb;', 'root', '123' );*/





    /*下面是将pdo进行了一个简单的封装*/

    $userclass = dirname(__FILE__) . '/source/module/User.php';

    require_once($userclass);

    $user = new User();



    /*t_testfile 表中file字段数据类型为longblod*/

    $sql="insert into t_testfile (file) values ('$file_data')";

    $result=$user->db->doSql($sql);



    //下面这句取出了刚才的insert语句的id

    $id=$user->db->getLastId();



    echo "上传成功--- ";

    echo "<a href='show_info.php?id=$id'>显示上传文件信息</a>";

}

else {

    echo "你没有上传任何文件";

}

}



/* function  doSql($sql, $model = 'many', $debug = false)

{

    if ($debug) echo $sql;

    $this->sth = $this->dbh->query($sql);

    $this->getPDOError();

    $this->sth->setFetchMode(PDO::FETCH_ASSOC);

    if ($model == 'many') {

        $result = $this->sth->fetchAll();

    } else {

        $result = $this->sth->fetch();

    }

    $this->sth = null;

    return $result;

}*/

?>





<!DOCTYPE html>

<html>

<head lang="en">

    <meta charset="UTF-8">

    <title>测试文件上传并存入到MySQL数据库中</title>

    <script src="../js/jquery-1.8.3.min.js"></script>



</head>

<body>

  <div>

      <form enctype='multipart/form-data' name='myform' action='uploadtest.php' method='post'>

            <input type="file" id="myfile" name="myfile" />

          <input type="submit" id="btnupload" value="上传文件"/>

      </form>

  </div>

</body>

</html>

 二、遇到问题:文件大于php默认大小是,上传获取不到文件,直接报“你没有上传任何文件”

  所以:调整php.ini配置文件中的参数,后重启Apache服务。

     调整的变量:

file_uploads = On ;打开文件上传选项

upload_max_filesize = 500M ;上传文件上限

如果要上传比较大的文件,仅仅以上两条还不够,必须把服务器缓存上限调大,把脚本最大执行时间变长

post_max_size = 500M ;post上限

max_execution_time = 1800 ; Maximum execution time of each script, in seconds脚本最大执行时间

max_input_time = 1800 ; Maximum amount of time each script may spend parsing request data

memory_limit = 128M ; Maximum amount of memory a script may consume (128MB)内存上限

三、点击上传又出现一个问题。MySQL server has gone away,由于英语很菜,直接有道翻译:‘MySQL服务器已经消失’

百度(还是要感谢百度,和广大牛人的分享)让我找到了解决方法,不至于迷失很久。

MySQL server has gone away解决方法:http://www.cnblogs.com/cenalulu/archive/2013/01/08/2850820.html

 

你可能感兴趣的:(mysql)