ホームページ > php教程 > php手册 > PHP のファイルアップロードサイズの制限

PHP のファイルアップロードサイズの制限

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

次のコードに示すように、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>body { </span></li><li><span>    margin-left: 00px; </span></li><li class="alt"><span>    margin-top: 0px; </span></li><li><span>    margin-right: 0px; </span></li><li class="alt"><span>    margin-bottom: 0px; </span></li><li><span>} </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">"385"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"185"</span><span> </span><span class="attribute">border</span><span>=</span><span class="attribute-value">"0"</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">"130"</span><span> </span><span class="attribute">height</span><span>=</span><span class="attribute-value">"75"</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">"200"</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">"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></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">"38"</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> </span></span></li>
<li class="alt"><span>    <span class="tag"><</span><span class="tag-name">input</span><span> </span><span class="attribute">name</span><span>=</span><span class="attribute-value">"file3"</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">"file3"</span><span> </span><span class="attribute">size</span><span>=</span><span class="attribute-value">"15"</span><span> </span><span class="attribute">maxlength</span><span>=</span><span class="attribute-value">"150"</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 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">"30"</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">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 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>   </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">"42"</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>?> </span></span></li>
<li class="alt"><span><?php </span></li><li><span> <span class="keyword">if</span><span>(</span><span class="vars">$Submit</span><span>==</span><span class="string">"提交"</span><span>){ </span></span></li><li class="alt"><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><span><span class="vars">$file_name</span><span>=</span><span class="string">"files"</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">'file3'</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>>2000000){ </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?lmbs=文件上传\">将在3秒钟后返回前页..."</span><span>; </span></span></li>
<li class="alt"><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">'file3'</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">'file3'</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">$query</span><span>=</span><span class="string">"insert into tb_file2(file_name,file_text,data)values('$file_name','$path','$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></li>
<li><span>    <span class="keyword">if</span><span>(</span><span class="vars">$result</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?lmbs=文件上传\">"</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?lmbs=文件上传\">"</span><span>;} </span></span></li>
<li class="alt"><span>}}} </span></li>
<li><span>?> </span></li>
</ol>
ログイン後にコピー



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