次のコードに示すように、PHP は画像アップロードの形式を制限します。
<ol class="dp-xml"> <li class="alt"><span><span class="tag"><</span><span class="tag-name">html</span><span class="tag">></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 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><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 class="alt"><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> <span><!-- </span></li><li class="alt"><span>.style1 { </span></li><li><span> font-size: 14px; </span></li><li class="alt"><span> font-family: "华文行楷"; </span></li><li><span>} </span></li><li class="alt"><span>.style4 {font-size: 12px; font-weight: bold; } </span></li><li><span>--<span class="tag">></span><span> </span> </li> <li class="alt"><span><span class="tag"></</span><span class="tag-name">style</span><span class="tag">></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><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">"406"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"129"</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">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">width</span><span>=</span><span class="attribute-value">"106"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"40"</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">"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></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">"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></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> </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></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">"32"</span><span> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"right"</span><span class="tag">></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>:</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 class="tag"><</span><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></li> <li class="alt"><span> <span class="tag"><</span><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></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> </span><span class="attribute">align</span><span>=</span><span class="attribute-value">"right"</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">span</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"style4"</span><span class="tag">></span><span>图片的格式</span><span class="tag"></</span><span class="tag-name">span</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 class="tag"><</span><span class="tag-name">span</span><span> </span><span class="attribute">class</span><span>=</span><span class="attribute-value">"style4"</span><span class="tag">></span><span>(.jpg)</span><span class="tag"></</span><span class="tag-name">span</span><span class="tag">></span><span> </span></span></li> <li class="alt"><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><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">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">"10"</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 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">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></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></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></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">$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><span><span class="vars">$type</span><span>=</span><span class="func">strstr</span><span>(</span><span class="vars">$path</span><span>,</span><span class="string">'.'</span><span>); </span></span></li><li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$type</span><span>!=</span><span class="string">".jpg"</span><span>){</span><span class="func">echo</span><span> </span><span class="string">"<script> alert('对不起,您上传的图片的格式不对!!'); history.back();</script>"</span><span>; </span> </li> <li><span>}<span class="keyword">else</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 http-equiv=\"Refresh\" content=\"3;url=index.php\">"</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 http-equiv=\"Refresh\" content=\"3;url=index.php\">"</span><span>;} </span></span></li> <li class="alt"><span>}}} </span></li> <li><span>?> </span></li> </ol>