Home  >  Article  >  php教程  >  PHP上传单张图片

PHP上传单张图片

WBOY
WBOYOriginal
2016-06-21 08:52:36814browse

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 class="tag-name">html</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"><span class="tag-name">head</span><span class="tag">></span><span> </span></span></span></li>
<li><span><span class="tag"><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></span></li>
<li class="alt"><span><span class="tag"><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></span></li>
<li><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 class="tag-name">body</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span><span class="tag"><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">"240"</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></span></li>
<li><span>  <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"106"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"100"</span><span class="tag">></span><span> </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"196"</span><span class="tag">></span><span> </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"31"</span><span class="tag">></span><span> </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><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 class="tag-name">form</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"form1"</span><span> </span><span class="attribute">method</span><span>=</span><span class="attribute-value">"post"</span><span> </span><span class="attribute">action</span><span>=</span><span class="attribute-value">"index_ok.php"</span><span> </span><span class="attribute">enctype</span><span>=</span><span class="attribute-value">"multipart/form-data"</span><span class="tag">></span><span> </span></span></span></li>
<li><span>  <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"30"</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></span></li>
<li><span>    <span class="tag"><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 class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"images"</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">"images2"</span><span> </span><span class="attribute">size</span><span>=</span><span class="attribute-value">"15"</span><span class="tag">></span><span> </span></span></span></span></li>
<li class="alt"><span>     <span class="tag"><span class="tag-name">input</span><span> </span><span class="attribute">type</span><span>=</span><span class="attribute-value">"hidden"</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"MAX_FILE_SIZE"</span><span> </span><span class="attribute">value</span><span>=</span><span class="attribute-value">"30000"</span><span class="tag">></span><span>   </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><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></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 class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"55"</span><span class="tag">></span><span> </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><span class="tag-name">td</span><span class="tag">></span><span class="tag"><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></span></span></li>
<li class="alt"><span>    <span class="tag"><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></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">form</span><span class="tag">></span><span> </span></span></li>
<li><span>  <span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"55"</span><span class="tag">></span><span> </span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><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></span></li>
<li class="alt"><span>    <span class="tag"><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></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 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">"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></span></li>
<li class="alt"><span> <span class="tag"></span><span class="tag-name">php</span><span> $</span><span class="attribute">query</span><span>=</span><span class="attribute-value">"select * from tb_image2 where id order by data 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($<span class="attribute">result</span><span>==true){ </span></span></li>
<li><span> while($<span class="attribute">myrow</span><span>=</span><span class="attribute-value">mysql_fetch_array</span><span>($result)){  </span></span></li>
<li class="alt"><span> <span class="tag">?></span><span class="tag"><span class="tag-name">tr</span><span class="tag">></span><span> </span></span></span></li>
<li><span>    <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"100"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</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></span></li>
<li class="alt"><span>    <span class="tag"><span class="tag-name">td</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"255"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"center"</span><span class="tag">></span><span class="tag"><span class="tag-name">img</span><span> </span><span class="attribute">src</span><span>=</span><span class="attribute-value">"<?php  echo $myrow[path];?>"</span><span> </span><span class="attribute">width</span><span>=</span><span class="attribute-value">"200"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"150"</span><span class="tag">></span><span class="tag"></span><span class="tag-name">td</span><span class="tag">></span><span> </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">php</span><span> }}</span><span class="tag">?></span><span> </span></span></li>
<li><span><span class="tag"></span><span class="tag-name">table</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span class="tag"></span><span class="tag-name">body</span><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>
</ol>

conn.php:

<ol class="dp-c">
<li class="alt"><span><span><?php   </span></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></li>
</ol>

index_ok.php:

<ol class="dp-c">
<li class="alt"><span><span><?php   </span></span></span></li>
<li><span>session_start();  </span></li>
<li class="alt"><span><span class="keyword">include</span><span>(</span><span class="string">"conn.php"</span><span>);?> </span></span></li>
<li><span><?php  </span></span></li>
<li class="alt"><span> <span class="keyword">if</span><span>(</span><span class="vars">$Submit</span><span>==</span><span class="string">"提交"</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">$filesize</span><span>=</span><span class="vars">$_FILES</span><span>[</span><span class="string">'images'</span><span>][</span><span class="string">'size'</span><span>]; </span></span></li>
<li><span><span class="keyword">if</span><span>(</span><span class="vars">$filesize</span><span>>1000000) </span></span></li>
<li class="alt"><span>  {<span class="func">echo</span><span> </span><span class="string">"<script> alert('对不起,您输入的图片太大,不能上传!!'); history.back();</script>"</span><span>;}</span><span class="keyword">else</span><span>{ </span></span></li>
<li><span><span class="vars">$path</span><span> = </span><span class="string">'upfiles/'</span><span>. </span><span class="vars">$_FILES</span><span>[</span><span class="string">'images'</span><span>][</span><span class="string">'name'</span><span>]; </span></span></li>
<li class="alt"><span><span class="keyword">if</span><span> (move_uploaded_file(</span><span class="vars">$_FILES</span><span>[</span><span class="string">'images'</span><span>][</span><span class="string">'tmp_name'</span><span>],</span><span class="vars">$path</span><span>)) {  </span></span></li>
<li><span>    <span class="vars">$query1</span><span>=</span><span class="string">"insert into tb_image2(path,data,image_name)values('$path','$data','$images')"</span><span>; </span></span></li>
<li class="alt"><span>    <span class="vars">$result1</span><span>=mysql_query(</span><span class="vars">$query1</span><span>); </span></span></li>
<li><span>    <span class="keyword">if</span><span>(</span><span class="vars">$result1</span><span>=true){  </span></span></li>
<li class="alt"><span>    <span class="func">echo</span><span> </span><span class="string">"上传成功!!"</span><span>; </span></span></li>
<li><span>    <span class="func">echo</span><span> </span><span class="string">"<meta>"</span><span>;  </span></span></li>
<li class="alt"><span>    }<span class="keyword">else</span><span>{</span><span class="func">echo</span><span> </span><span class="string">"文件上传失败!!"</span><span>; </span></span></li>
<li><span>          <span class="func">echo</span><span> </span><span class="string">"<meta>"</span><span>;} </span></span></li>
<li class="alt"><span>}}} </span></li>
<li><span>?> </span></li>
</ol>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn