ホームページ > php教程 > php手册 > PHP は画像をバイナリ形式でデータベースにアップロードします

PHP は画像をバイナリ形式でデータベースにアップロードします

WBOY
リリース: 2016-06-21 08:52:37
オリジナル
1132 人が閲覧しました

次のコードに示すように、PHP は画像をバイナリ形式でデータベースにアップロードします。

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span>  </span></span></li><li><span>session_start();  </span></li><li class="alt"><span>include("conn.php");<span class="tag">?></span><span> </span></span></li>
<li><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"><</span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"><</span><span class="tag-name">meta</span><span> </span><span class="attribute">http-equiv</span><span>=</span><span class="attribute-value">"Content-Type"</span><span> </span><span class="attribute">content</span><span>=</span><span class="attribute-value">"text/html; charset=gb2312"</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"><</span><span class="tag-name">title</span><span class="tag">></span><span>以二进制形式上传图片到数据库 - www.cxybl.com</span><span class="tag"></</span><span class="tag-name">title</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"><</span><span class="tag-name">style</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"text/css"</span><span class="tag">></span><span> </span></span></li>
<li class="alt">
<span><!-- </span></li><li><span>body { </span></li><li class="alt"><span>    margin-left: 0px; </span></li><li><span>    margin-top: 0px; </span></li><li class="alt"><span>    margin-right: 0px; </span></li><li><span>    margin-bottom: 0px; </span></li><li class="alt"><span>} </span></li><li><span>.style1 {font-size: 12px} </span></li><li class="alt"><span>--<span class="tag">></span><span> </span>
</li>
<li><span><span class="tag"></</span><span class="tag-name">style</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">head</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span> </span></li>
<li><span><span class="tag"><</span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"><</span><span class="tag-name">table</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"355"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"222"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span> </span><span class="attribute">cellpadding</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">cellspacing</span><span>=</span><span class="attribute-value">"0"</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"><</span><span class="tag-name">form</span><span> </span><span class="attribute">action</span><span>=</span><span class="attribute-value">"index_ok.php"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"post"</span><span> </span><span class="attribute">enctype</span><span>=</span><span class="attribute-value">"multipart/form-data"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"form1"</span><span> </span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>  <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"110"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"65"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"right"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"184"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"61"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>  <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>  <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"25"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"right"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">valign</span><span>=</span><span class="attribute-value">"middle"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">valign</span><span>=</span><span class="attribute-value">"middle"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li><span>  <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>  <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"35"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"left"</span><span> </span><span class="attribute">valign</span><span>=</span><span class="attribute-value">"middle"</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"image"</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"file"</span><span> </span><span class="attribute">id</span><span>=</span><span class="attribute-value">"image"</span><span> </span><span class="attribute">size</span><span>=</span><span class="attribute-value">"15"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li><span>    <span class="tag"><</span><span class="tag-name">td</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>  <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>  <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"44"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span> </span><span class="attribute">valign</span><span>=</span><span class="attribute-value">"middle"</span><span class="tag">></span><span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"submit"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"Submit"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"提交"</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><</span><span class="tag-name">td</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li><span>  <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>  <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"52"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span> </span><span class="attribute">valign</span><span>=</span><span class="attribute-value">"top"</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li><span>    <span class="tag"><</span><span class="tag-name">td</span><span class="tag">></span><span> </span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>  <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"></</span><span class="tag-name">form</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"></</span><span class="tag-name">table</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"><</span><span class="tag-name">table</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"355"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span> </span><span class="attribute">cellpadding</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">cellspacing</span><span>=</span><span class="attribute-value">"0"</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>  <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>    <span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li class="alt"><span>     $<span class="attribute">query</span><span>=</span><span class="attribute-value">"select * from tb_image1 where data order by id desc limit 2  "</span><span>; </span></span></li><li><span>     $<span class="attribute">result</span><span>=</span><span class="attribute-value">mysql_query</span><span>($query); </span></span></li><li class="alt"><span>     if($result){ </span></li><li><span>     while ($<span class="attribute">myrow</span><span>=</span><span class="attribute-value">mysql_fetch_array</span><span>($result)){   </span><span class="tag">?></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"175"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span class="tag">></span><span> </span></span></li>
<li><span>     <span class="tag"><</span><span class="tag-name">table</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"175"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"1"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span> </span><span class="attribute">cellpadding</span><span>=</span><span class="attribute-value">"0"</span><span> </span><span class="attribute">cellspacing</span><span>=</span><span class="attribute-value">"0"</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>      <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>        <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"175"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>     <span class="tag"><</span><span class="tag-name">img</span><span> </span><span class="attribute">src</span><span>=</span><span class="attribute-value">"image_1.php?recid=<?php echo $myrow[id];?>"</span><span>  </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"170"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"100"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"0"</span><span class="tag">></span><span> </span></span></li>
<li><span>      <span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>        </span></li>
<li><span>      <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>      <span class="tag"><</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li><span>        <span class="tag"><</span><span class="tag-name">td</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"style1"</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>         <span class="tag"><</span><span class="tag-name">span</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"style1"</span><span class="tag">></span><span>上传时间:</span><span class="tag"></</span><span class="tag-name">span</span><span class="tag">></span><span class="tag"><?</span><span class="tag-name">php</span><span> echo $myrow[data];</span><span class="tag">?></span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></li>
<li><span>      <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span>    <span class="tag"></</span><span class="tag-name">table</span><span class="tag">></span><span class="tag"></</span><span class="tag-name">td</span><span class="tag">></span><span> </span><span class="tag"><?</span><span class="tag-name">php</span><span> }} </span><span class="tag">?></span><span> </span></span></li>
<li><span>  <span class="tag"></</span><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"></</span><span class="tag-name">table</span><span class="tag">></span><span> </span></span></li>
<li><span><span class="tag"></</span><span class="tag-name">body</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"></</span><span class="tag-name">html</span><span class="tag">></span><span> </span></span></li>
</ol>
ログイン後にコピー

conn.php:

<ol class="dp-c"><li class="alt"><span><span><?php  </span></span></li><li><span><span class="vars">$id</span><span>=mysql_connect(</span><span class="string">'localhost'</span><span>,</span><span class="string">'root'</span><span>,</span><span class="string">'root'</span><span>); </span></span></li><li class="alt"><span>mysql_select_db(<span class="string">"db_database12"</span><span>,</span><span class="vars">$id</span><span>); </span></span></li><li><span>mysql_query(<span class="string">"set names gb2312"</span><span>); </span></span></li><li class="alt"><span>?> </span></span></li></ol>
ログイン後にコピー

index_ok.php:

<ol class="dp-c">
<li class="alt"><span><span><?php  </span><span class="keyword">include</span><span>(</span><span class="string">"conn.php"</span><span>);?> </span></span></li>
<li>
<span><?php  <span class="keyword">if</span><span>(</span><span class="vars">$Submit</span><span>==true){ </span></span></li><li class="alt"><span>        <span class="vars">$names</span><span>=</span><span class="string">"images"</span><span>; </span></span></li><li><span>        <span class="vars">$data</span><span>=</span><span class="func">date</span><span>(</span><span class="string">"y;m;d"</span><span>); </span></span></li><li class="alt"><span>        <span class="vars">$fp</span><span>=</span><span class="func">fopen</span><span>(</span><span class="vars">$image</span><span>,</span><span class="string">"rb"</span><span>); </span></span></li><li><span>        <span class="vars">$image</span><span>=</span><span class="func">addslashes</span><span>(</span><span class="func">fread</span><span>(</span><span class="vars">$fp</span><span>,</span><span class="func">filesize</span><span>(</span><span class="vars">$image</span><span>)));     </span></span></li><li class="alt"><span>           <span class="keyword">if</span><span>(</span><span class="vars">$image</span><span>==true){ </span></span></li><li><span>        <span class="vars">$query</span><span>=</span><span class="string">"insert into tb_image1 (image_name,image,data)values('$names','$image','$data')"</span><span>; </span></span></li><li class="alt"><span>        <span class="vars">$result</span><span>=mysql_query(</span><span class="vars">$query</span><span>,</span><span class="vars">$id</span><span>);  </span></span></li><li><span>              <span class="func">echo</span><span> </span><span class="string">"<meta http-equiv=\"refresh\" content=\"1;url=index.php\">请稍等..."</span><span>; </span>
</li>
<li class="alt"><span>           }<span class="keyword">else</span><span>{ </span></span></li>
<li><span>              <span class="func">echo</span><span> </span><span class="string">"<meta http-equiv=\"refresh\" content=\"1;url=index.php\">请输入图片..."</span><span>; }  } </span></span></li>
<li class="alt"><span>?> </span></li>
</ol>
ログイン後にコピー



関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート