我们都知道PHP有强大的功能,给我带来了许多方便,我们这里来看看PHP上传文件类型的实现。PHP支持上传文件的功能,但并不是所有支持PHP的网站都支持此功能,特别是免费的网站。
实现上传,首先要在HTML上加入上传文件的“<FORM>”表格。
<ol class="dp-xml"> <li class="alt"><span><span><form </span><span class="attribute"><font color="#ff0000">method</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">post</font></span><span> </span><span class="attribute"><font color="#ff0000">action</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"upload.php"</font></span><span> </span><span class="attribute"><font color="#ff0000">ENCTYPE</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"multipart/form-data"</font></span><span>> </span></span></li> <li class=""> <span><input </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"file"</font></span><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"upload_file"</font></span><span>> </span> </li> <li class="alt"> <span><input </span><span class="attribute"><font color="#ff0000">type</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"submit"</font></span><span> </span><span class="attribute"><font color="#ff0000">name</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"submit"</font></span><span> </span><span class="attribute"><font color="#ff0000">value</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">"上传文件"</font></span><span>> </span> </li> </ol>
用PHP上传文件类型时,需要对内容作详细的检查,例如是否容许读写文件,文件格式、文件大小是否在你指定的大小内等。
<ol class="dp-xml"> <li class="alt"><span><span><? </span></span></li> <li class=""> <span>$</span><span class="attribute">file_size_max</span><span> = </span><span class="attribute-value">1000000</span><span>; </span> </li> <li class="alt"><span>// 限制文件上传最大容量(bytes) </span></li> <li class=""> <span>$</span><span class="attribute">store_dir</span><span> = </span><span class="attribute-value">"/public/www/upload/"</span><span>; </span> </li> <li class="alt"><span>// 上传文件的储存位置 </span></li> <li class=""> <span>$</span><span class="attribute">accept_overwrite</span><span> = </span><span class="attribute-value">true</span><span>; </span> </li> <li class="alt"><span>//允许读写文件 </span></li> <li class=""><span>// 检查文件大小 </span></li> <li class="alt"><span>if ($upload_file_size > $file_size_max) { </span></li> <li class=""><span>echo "对不起,你的文件容量大于规定"; </span></li> <li class="alt"><span>exit; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>// 检查读写文件 </span></li> <li class=""><span>if (file_exists($store_dir . $upload_file_name) &&&& !$accept_overwrite) { </span></li> <li class="alt"><span>echo "文件已存在,不能再复制"; </span></li> <li class=""><span>exit; </span></li> <li class="alt"><span>} </span></li> <li class=""><span>//复制文件到指定目录 </span></li> <li class="alt"><span>if (! @ copy($upload_file,$store_dir . $upload_file_name)) { </span></li> <li class=""><span>echo "复制文件失败"; </span></li> <li class="alt"><span>exit; </span></li> <li class=""><span>} </span></li> <li class="alt"><span>echo "上传文件完成"; </span></li> <li class=""><span>?> </span></li> </ol>