php图片的二进制转换实例
这里我们是在上传文件时把上传的文件转换成二进制然后保存到数据的字段中去,下次读读出我们也用同样的方法显示即可。
html:
<form action="insertPic.php" method="post" enctype="multipart/form-data" name="mainForm" id="mainForm"> <input type="file" name="myFile" /> <input type="submit" name="Submit" value="Submit"/> </form>
将图片保存到数据库:
<?php //由于上传过来的图片被保存在一个临时文件中,所以 //我们仅需要读取该文件就可以获取传过来的图片 $fp = fopen($_FILES["myFile"]["tmp_name"], "rb"); $buf = addslashes(fread($fp, $_FILES["myFile"]["size"])); //创建一个PDO对象 $dbh = new PDO("mysql:host=localhost;port= 3306;dbname=test", "root", "123456"); //执行插入操作并将结果保存在一个变量中 $result = $dbh->query("INSERT INTO img (images) VALUES (‘$buf’)"); //获取影响的行数 if ($result->rowCount() > 0) { echo ("数据已插入。"); } else { echo ("不能执行插入操作。"); } //显式的关闭PDO连接 $dbh = NULL; ?>
显示图片:(show.php)
<?php $conn = @mysql_connect("localhost", "root", "123456") or die("服务器连接错误!"); //链接数据库 @mysql_select_db("test", $conn) or die("未发现数据库!"); $query = "select * from img where Id=" . $_GET['id']; $result = mysql_query($query); www . phprm . com $num = mysql_num_rows($result); $data = mysql_result($result, 0, "images"); header("Content-type: image/" . $num['imgType']); echo $data; ?>
或者
1、fopen函数。
fopen()函数用于打开文件或者URL。语法如下:
int fopen(string filename, string mode);
字符串参数mode可以是下列的情形:
"r"开文件方式为只读,文件指针指到开始处。
"r+"开文件方式为可读写,文件指针指到开始处。
"w"开文件方式为写入,文件指针指到开始处,并将原文件的长度设为0。若文件不存在,则建立新文件。
"w+"开文件方式为可读写,文件指针指到开始处,并将原文件的长度设为0。若文件不存在,则建立新文件。
"a"开文件方式为写入,文件指针指到文件最后。若文件不存在,则建立新文件。
"a+"开文件方式为可读写,文件指针指到文件最后。若文件不存在,则建立新文件。
"b"若操作系统的文字及二进位文件不同,则可以用此参数,UNIX系统不需要使用本参数。
2、Addslashes函数。
Addslashes函数用于将字符串加入斜线。语法如下:【注:我测试的时候是把这个函数去掉后成功了,道理你懂的。自己试下就明白了】
string addslashes(string str);
该函数使需要让数据库处理的字符串,引号的部份加上斜线,以供数据库查询(query)能顺利运作。这些会被改的字符包括单引号(’)、双引号(")、反斜线backslash()以及空字符NULL(the null byte)。
3、fread函数。
fread函数用于读到指定长度的位组或到文件尾EOF。语法如下:
string fread(int fp, int length);可安全用于二进制文件
fread() 从文件指针 file 读取最多 length 个字节。该函数在读取完最多 length 个字节数,或到达 EOF 的时候,或(对于网络流)当一个包可用时,或(在打开用户空间流之后)已读取了 8192 个字节时就会停止读取文件,视乎先碰到哪种情况。
二进制转换成图片
注:$newFilePath 对生成的图片名和路径做处理,这里自己去实现。
$newFilePath='1.jpg'; $data = $GLOBALS[HTTP_RAW_POST_DATA];//得到post过来的二进制原始数据 if(empty($data)){ www.phprm.com $data=file_get_contents("php://input"); } $newFile = fopen($newFilePath,"w");//打开文件准备写入 fwrite($newFile,$data);//写入二进制流到文件 fclose($newFile);//关闭文件
可以把读取到的二进制流存到数据库,也可以直接写入成一个图片。
获取二进制头文件,从而得知属于什么类型文件
<?php $bin = substr($content, 0, 2); $strInfo = @unpack("C2chars", $bin); $typeCode = intval($strInfo['chars1'] . $strInfo['chars2']); $fileType = ''; switch ($typeCode) { case 7790: $fileType = 'exe'; break; case 7784: $fileType = 'midi'; break; case 8297: $fileType = 'rar'; break; case 255216: $fileType = 'jpg'; break; case 7173: $fileType = 'gif'; break; case 6677: $fileType = 'bmp'; break; case 13780: $fileType = 'png'; break; default: echo 'unknown'; } ?>
本文链接:
收藏随意^^请保留教程地址.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to solve PHPWarning:fopen():SSLoperationfailedinfile.phponlineX In PHP programming, we often use the fopen function to open files or URLs and perform related operations. However, when using the fopen function, sometimes you will encounter something similar to Warning:fopen():SSLoperationfailedinfile.p

How to solve PHPWarning:fopen():failedtoopenstream:Permissiondenied In the process of developing PHP programs, we often encounter some error messages, such as PHPWarning:fopen():failedtoopenstream:Permissiondenied. This error is usually due to incorrect file or directory permissions

How to solve PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory In the process of using PHP development, we often encounter some file operation problems, one of which is "PHPWarning:fopen():failedtoopenstream:Nosuchfileordirectory"

In Matlab, the fopen function is used to open a file and return the file identifier for subsequent reading or writing operations on the file. Select the appropriate permission options to open the file as needed, and promptly close the file when the operation is complete. It should be noted that after opening a file, you need to ensure that the file is closed in time when it is no longer needed to release system resources. In addition, if the file opening fails or an operation error occurs, the error handling mechanism can be used to handle it accordingly.

Asynchronous processing method of SelectChannelsGo concurrent programming using golang Introduction: Concurrent programming is an important area in modern software development, which can effectively improve the performance and responsiveness of applications. In the Go language, concurrent programming can be implemented simply and efficiently using Channels and Select statements. This article will introduce how to use golang for asynchronous processing methods of SelectChannelsGo concurrent programming, and provide specific

How to hide the select element in jquery: 1. hide() method, introduce the jQuery library into the HTML page, you can use different selectors to hide the select element, the ID selector replaces the selectId with the ID of the select element you actually use; 2. css() method, use the ID selector to select the select element that needs to be hidden, use the css() method to set the display attribute to none, and replace selectId with the ID of the select element.

jQuery is a popular JavaScript library that can be used to simplify DOM manipulation, event handling, animation effects, etc. In web development, we often encounter situations where we need to change event binding on select elements. This article will introduce how to use jQuery to bind select element change events, and provide specific code examples. First, we need to create a dropdown menu with options using labels:

The fopen() method in C is used to open the specified file. Let's take an example to understand the problem syntax FILE*fopen(filename,mode). The following are valid modes for using fopen() to open files: 'r', 'w', 'a', 'r+', 'w+', ' a+'. For more information, please visit the C library function-fopen()
