当前位置:网站首页>php文件上传下载(存放文件二进制到数据库)
php文件上传下载(存放文件二进制到数据库)
2022-08-08 12:35:00 【51CTO】
php读取mysql二进制图片
1将文件存到项目路径下
2.如果你想把二进制的数据,比如说图片文件和HTML文件,直接保存在你的MySQL数据库,那么这篇文章就是为你而写的!我将告诉你怎样通过HTML表单来储存这些文件,怎样访问和使用这些文件。
一、本文概述
本文的主要内容如下:
* 在MySQL中建立一个新的数据库
* 一个怎样储存文件的例子程序
* 一个怎样访问文件的例子程序
二、在MySQL中建立一个新的database
首先,你必须在你的MySQL中建立一个新的数据库,我们将会把那些二进制文件储存在这个数据库里。在例子中我会使用下列结构,为了建立数据库,你必须做下列步骤:
1. 进入MySQL控制器
2. 输入命令"create database binary_data;"
3. 输入命令"use binary_data;"
输入如下命令:
(3)submit.php//把用户上传得文件连同文件的基本信息保存到数据库里
<?php
if (
$_FILES [
'myfile']
!=
"
none"
&&
$_FILES [
'myfile']
!=
"
") {
$time_limit
=
60;
set_time_limit (
$time_limit );
$file_type
=
$_FILES [
'myfile'] [
'type'];
$file_name
=
$_FILES [
'myfile'] [
'name'];
$file_size
=
$_FILES [
'myfile'] [
'size'];
$fp
=
fopen (
$_FILES [
'myfile'] [
'tmp_name'],
"
rb" );
if (
!
$fp)
die (
"
file open error" );
$file_data
=
addslashes (
fread (
$fp,
$file_size ) );
fclose (
$fp );
/** 等价
$file_data = file_get_contents($_FILES["myfile"]['tmp_name']);
*/
$conn
=
mysql_connect (
"
localhost",
"
root",
"
root" );
if (
!
$conn)
die (
"
error : mysql connect failed" );
mysql_select_db (
"
test",
$conn );
$sql
=
"
insert into receive(file_data,file_type,file_name,file_size)values ('
$file_data
','
$file_type
','
$file_name
',
$file_size
)";
$result
=
mysql_query (
$sql );
$id
=
mysql_insert_id ();
mysql_close (
$conn );
set_time_limit (
30 );
//恢复缺省超时设置
echo
"
上传成功--- ";
echo
"
<a href='show_info.php?id=
$id
'>显示上传文件信息</a>";
}
else {
echo
"
你没有上传任何文件";
}
?>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
(4)show_info.php //从数据库里取出文件的基本信息[文件名和文件大小]
<?php
$id
=
$_REQUEST [
"
id"];
if (
!
isset (
$id )
or
$id
==
"
")
die (
"
error:id none" );
//定位记录,读出
$conn
=
mysql_connect (
"
localhost",
"
root",
"
root" );
if (
!
$conn)
die (
"
error:mysql connect failed" );
mysql_select_db (
"
test",
$conn );
$sql
=
"
select file_name,file_size from receive where id=
$id
";
$result
=
mysql_query (
$sql );
if (
!
$result)
die (
"
error:mysql query" );
//如果没有指定的记录,则报错
$num
=
mysql_num_rows (
$result );
if (
$num
<
1)
die (
"
error:no this recorder" );
$name
=
mysql_result (
$result,
0,
"
file_name" );
$size
=
mysql_result (
$result,
0,
"
file_size" );
mysql_close (
$conn );
echo
"
<hr>上传的文件的信息:";
echo
"
<br>文件名称:
$name
";
echo
"
<br>文件大小:
$size
";
echo
"
<br><a href=show_add.php?id=
$id
>下载</a>";
?>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
(5)show_add.php //从数据库里取出文件内容
<?php
$id
=
$_REQUEST [
"
id"];
if (
!
isset (
$id )
or
$id
==
"
")
die (
"
error:id none" );
//定位记录,读出
$conn
=
mysql_connect (
"
localhost",
"
root",
"
root" );
if (
!
$conn)
die (
"
error:mysql connect failed" );
mysql_select_db (
"
test",
$conn );
$sql
=
"
select * from receive where id=
$id
";
$result
=
mysql_query (
$sql );
if (
!
$result)
die (
"
error:mysql query" );
$num
=
mysql_num_rows (
$result );
if (
$num
<
1)
die (
"
error:no this recorder" );
$data
=
mysql_result (
$result,
0,
"
file_data" );
$type
=
mysql_result (
$result,
0,
"
file_type" );
$fileName
=
mysql_result (
$result,
0,
"
file_name" );
mysql_close (
$conn );
$filePath
=
'download/' .
$userid .
"
/" .
$fileName;
$file
=
fopen (
RelativePath .
$filePath,
"
wb" );
if (
fwrite (
$file,
$data )
===
FALSE) {
echo
"
can not write in " .
$file;
exit ();
}
fclose (
$file );
header (
'Location: /download.php?fileName=' .
$fileName .
'&file=' .
$filePath );
/*
download.php
$fileName = $_GET['fileName'];
$file_dir = RelativePath.$_GET['file'];
$file = @ fopen($file_dir ,"r");
if (!$file) {
echo "File does not exist.";
} else {
Header("Content-type: application/octet-stream");
Header("Content-Disposition: attachment; filename=" .$fileName);
while (!feof ($file)) {
echo fread($file,50000);
}
fclose ($file);
}
*/
?>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
(6)list.php //显示所有文件清单
<?php
if (
!
mysql_connect (
"
localhost",
"
root",
"
root" ))
die (
"
error:mysql connect failed" );
$sql
=
"
select file_name,file_size,id from test.receive";
$result
=
mysql_query (
$sql );
if (
!
$result)
die (
"
error:mysql query" );
while (
$row
=
mysql_fetch_array (
$result ) ) {
echo
"
<hr>上传的文件的信息:";
echo
"
<br>文件名称:
$row[
file_name]
";
echo
"
<br>文件大小:
$row[
file_size]
";
echo
"
<br><a href=show_add.php?id=
$row[
id]
>下载</a>";
}
mysql_free_result (
$result );
mysql_close ();
?>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
边栏推荐
猜你喜欢
随机推荐
C语言的三个经典题目:三步翻转法、杨氏矩阵、辗转相除法
SSTI漏洞介绍认识(flask、Werkzeup)
node中package解析、npm 命令行npm详解,node中的common模块化,npm、nrm两种方式查看源和切换镜像
【C语言】文件相关操作
SQL实例 - 胜平负
Promise 解决阻塞式同步,将异步变为同步
简短截说阐述redis中事务的使用
面试突击72:输入URL之后会执行什么流程?
SQL INSERT INTO and INSERT INTO the SELECT statement
MySQL----索引
高可用的并行MySQL数据同步及分布式
Three classic topics in C language: three-step flip method, Young's matrix, and tossing and dividing method
[C language] file related operations
一文搞懂│XSS攻击、SQL注入、CSRF攻击、DDOS攻击、DNS劫持
Background, History and Lessons of Transfer Learning
curl获取harbor镜像仓库项目下的镜像列表
C language small project -- address book (static version + dynamic version + file version)
(4) FlinkSQL writes socket data to mysql Method 1
Jenkins - 持续集成介绍(1)
(原创)[C#] GDI+ 之鼠标交互:原理、示例、一步步深入、性能优化








